Question: 1. Write a C program to fork a child process with a fork() and print the process id for a child and for a parent

1. Write a C program to fork a child process with a fork() and print the process id for a child and for a parent process using a C library function getpid(). Create an int variable that should be incremented by a child process and decremented by a parent process. Print the value of the variable for both a child and a parent. 1 Note: a child process id upon returning from fork() is 0. 2. Run a program, comment on the received ids and the values of the variable. Explain. 3. Trace system calls used by your program using strace nameofyourfile (-c option gives a summary of system calls only) 4. Identify a system call responsible for creation of your child process. 5. Modify your program so that your child process creates a thread with the pthread_create call. In a function that a thread will execute print the thread id using the pthread_self() call. 6. Submit your program and explanation.

Thread Information To include the pthread.h library: #include To declare a variable of type pthread_t : pthread_t mythread; The function pthread_create() is used to create a new thread, and a thread to terminate itself uses the function pthread_exit(). A thread to wait for termination of another thread uses the function pthread_join. int pthread_create ( pthread_t * threadhandle, /* Thread handle returned by reference */ pthread_attr_t *attribute, /* Special Attribute for starting thread */ keep it as NULL void *(*start_routine)(void *), /* Main Function which thread executes */ void *arg /* An extra argument passed as a pointer */ keep it as NULL ); int pthread_join ( 1 You can read more about fork(), getpid() and other C library functions by using man fork, man getpid etc.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!