Question

In: Computer Science

Please look at the following code. When I run it from the command line, I am...

Please look at the following code. When I run it from the command line, I am supposed to get the following results:

1: I am
1: I am

I need to fix it so that the functions 'print_i' and 'print_j' print all of their lines. What do I need to add? Thank you.

C source code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>

// These two functions will run concurrently
void* print_i(void *ptr)
{
printf("1: I am \n");
sleep(1);
printf("in i\n");
}

void* print_j(void *ptr)
{
printf("2: I am \n");
sleep(1);
printf("in j\n");
}

int main() {

pthread_t t1,t2;
int rc1 = pthread_create(&t1, NULL, print_i, NULL);
int rc2 = pthread_create(&t2, NULL, print_j, NULL);

exit(0);
}

Solutions

Expert Solution

Step(1)

First Of all You Try To implement A thread Concept ,let me elaborate Thread First then I give Solution For your Code As well.

A thread of execution is a sequence of instructions that can be executed concurrently with other such sequences in multithreading environments, while sharing a same address space.

An initialized thread object represents an active thread of execution; Such a thread object is joinable, and has a unique thread id.

A default-constructed (non-initialized) thread object is not joinable, and its thread id is common for all non-joinable threads.

Step(2)

I Try to write a code in C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>

// These two functions will run concurrently
void* print_i(void *threadid)
{
printf("1: I am \n");
sleep(1);
printf("in i\n");
}

void* print_j(void *threadid)
{
printf("2: I am \n");
sleep(1);
printf("in j\n");
}

int main()
{

pthread_t t1,t2;
int rc1 = pthread_create(&t1, NULL, print_i, (void *)1);
int rc2 = pthread_create(&t2, NULL, print_j, (void *)2);

// Wait for the threads to finish
pthread_join(t1, NULL);
pthread_join(t2, NULL);

exit(0);
}

Output

1: I am                                                                                                                                                    

1: I am                                                                                                                                                    

in i                                                                                                                                                       

in i  

Step(3)

SceenShot Of Program

Output

IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

Compile and run the following code then answer the following questions: a.) Run this command from...
Compile and run the following code then answer the following questions: a.) Run this command from the shell prompt: ./a.out ls -F Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Be sure to comment on the pointer arithmetic done inside the line: execvp(*(argv+1), argv+1); b.) Run this command from the shell prompt:...
Here is my fibonacci code using pthreads. When I run the code, I am asked for...
Here is my fibonacci code using pthreads. When I run the code, I am asked for a number; however, when I enter in a number, I get my error message of "invalid character." ALSO, if I enter "55" as a number, my code automatically terminates to 0. I copied my code below. PLEASE HELP WITH THE ERROR!!! #include #include #include #include #include int shared_data[10000]; void *fibonacci_thread(void* params); void parent(int* numbers); int main() {    int numbers = 0; //user input....
This is my code for python. I am trying to do the fourth command in the...
This is my code for python. I am trying to do the fourth command in the menu which is to add an employee to directory with a new phone number. but I keep getting error saying , "TypeError: unsupported operand type(s) for +: 'dict' and 'dict". Below is my code. What am I doing wrong? from Lab_6.Employee import * def file_to_directory(File): myDirectory={}       with open(File,'r') as f: data=f.read().split('\n')    x=(len(data)) myDirectory = {} for line in range(0,199):      ...
Using OpenSSL from the command line interface. Please solve all the following questions. I also need...
Using OpenSSL from the command line interface. Please solve all the following questions. I also need the screenshots for every part. a. Create a text file with some input and encrypt it using i. AES-128 CBC ii. AES-256 CTR iii. DES b. Create a 2048 bit RSA public and private key
How do I add additional command line arguments in C++? I am working on a programming...
How do I add additional command line arguments in C++? I am working on a programming assignment that has the user input a file into the command line and then they have the option to also add a series of other arguments to the command line. I know how to accept the text file from the command line by using: int main(int argc, char *argv[]) { /.../ } Then filename(argv[1]) would be the text file that they put into the...
C# programming. Comment/Explain the below code line by line. I am having a hard time following...
C# programming. Comment/Explain the below code line by line. I am having a hard time following it. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Nth_prime {     class Program     {         public static bool isPrime(int number)         {             int counter = 0;             for (int j = 2; j < number; j++)             {                 if (number % j == 0)                 {                     counter = 1;                     break;                 }             }             if (counter == 0)             {                 return true;             }             else             {                 return false;             }         }...
Using Python, I am trying to integrate 'iloc' into the line of code below? This line...
Using Python, I am trying to integrate 'iloc' into the line of code below? This line is replacing all the '1' values across my .csv file and is throwing off my data aggregation. How would I implement 'iloc' so that this line of code only changes the '1's integers in my 'RIC' column? patient_data_df= patient_data_df.replace(to_replace=[1], value ="Stroke") Thank you:)
The following code takes in a command line argument and copies it into a buffer before...
The following code takes in a command line argument and copies it into a buffer before working on it. Explain in detail any problems that you see in the following C code and how you would fix it. Note only describe how you would fix it, do not actually rewrite or give me another version of the code. void bad_function(char *input) { char dest_buffer[32]; char input_len = strlen(input); if (input_len < 32) { strcpy(dest_buffer, input_len); printf(“Command line argument is %s.\n”,dest_buffer);...
This is in Python I am getting an error when I run this program, also I...
This is in Python I am getting an error when I run this program, also I cannot get any output. Please help! #Input Section def main(): name=input("Please enter the customer's name:") age=int(input("Enter age of the customer: ")) number_of_traffic_violations=int(input("Enter the number of traffic violations: ")) if age <=15 and age >= 105: print('Invalid Entry') if number_of_traffic_violations <0: print('Invalid Entry') #Poccessing Section def Insurance(): if age < 25 and number_of_tickets >= 4 and riskCode == 1: insurancePrice = 480 elif age >=...
I have written code in C programming that checks where the command line arguments are floats...
I have written code in C programming that checks where the command line arguments are floats or not. For example, if I type "./math 1 1 0 0 2.5 3" in the terminal, my program realizes they are all floats but if I type "./math 1 1 0 0 2.5 g", it recognizes that not all arguments are floats and gives an error message. I want to take my code further such that after typing in "./math 1 1 0...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT