loading...

CS604 Operating System Assignment No 2 Solution Fall 2012

Question : [Marks 20]
Use C language to write a program. In that program main process creates a child process by using fork( ). Child process should perform the following tasks.
1) Take VU student id from user and then display the VU student id
2) Child process should display its PID(process ID)
3) Child process should display its parent’s PID
4) Child process should display value of its childpid variable (childpid is used to store the value returned by
fork( ))
5) Child process should take any value from 0 to 255 from user. After that child process exits and returns that value to the parent process.
Parent process should perform the following tasks
1) Parent process should display its PID(process ID)
2) Parent process should display value of copy of its childpid variable (childpid is used to store the value returned by fork( ))
3) When child process terminates then parent process receives a value from 0 to 255 from child process and then parent process displays it.
Sample out put is given below.
SAMPLE OUTPUT
PARENT: I am the parent process!
PARENT: Here’s my PID: 2861
PARENT: The value of my copy of childpid is 2862
PARENT: I will now wait for my child to exit.
CHILD: Enter your student id: bc12356487
CHILD: Student id is: bc12356487
CHILD: I am the child process!
CHILD: Here’s my PID: 2862
CHILD: My parent’s PID is: 2861
CHILD: The value of my copy of childpid is: 0
CHILD: Sleeping for 1 second…
CHILD: Enter any value from 0 to 255: 35
CHILD: Goodbye!
PARENT: Value returned by child code is: 35
PARENT: Goodbye!
Execute your program in Linux Ubuntu. Take screen shots of your program that you write in C, Commands that
you used to run your program in Linux Ubuntu and the out put displayed by your program. After taking screen
shots, send these screen shots to me properly in your assignment.
Note:
1) Do not write your program in other languages like Java, C++ etc. Write your program only in C language.
2) Do not execute your program in other operating systems like Windows, Mac etc. Execute your program only
in Linux Ubuntu flavor.
3) You should provide three screen shots. Required three screen shots that you should provide are given below.
i) Screen shots of your compete program that you write in C. Your screen shots should display your complete
program code that you write in C.
ii) Screen shot of Commands that you used to run your program in Linux Ubuntu.
iii) Screen shot of final output of your program shown.
4) If you will copy your assignment then you will get zero marks with no leniency.
5) Each student should display his own complete VU student ID and not the VU student ID of any other student.
6) In your program’s output, each student should indicate the execution of parent and child processes codes by
using the words PARENT: and CHILD: in the output display as shown in the sample output.

Answers :

#include <unistd.h> 
#include <sys/types.h> 
#include <errno.h> 
#include <stdio.h> 
#include <sys/wait.h> 
#include <stdlib.h> 

int main()
{
pid_t childpid; 
int retval; 
int status;
childpid = fork();

if (childpid >= 0) 
{
if (childpid == 0) 
{
char id[100];
printf("CHILD: Enter you student ID:n");
scanf("%s",id);
printf("Student ID is %s n", id);
printf("CHILD: I am the child process!n");
printf("CHILD: Here's my PID: %dn", getpid());
printf("CHILD: My parent's PID is: %dn", getppid());
printf("CHILD: The value of my copy of childpid is: %dn", childpid);
printf("CHILD: Sleeping for 1 second.n");
sleep(1); 
printf("CHILD: Enter an exit value (0 to 255): ");
scanf(" %d", &retval);
printf("CHILD: Goodbye!n"); 
exit(retval);
}
else
{
printf("PARENT: I am the parent process!n");
printf("PARENT: Here's my PID: %dn", getpid());
printf("PARENT: The value of my copy of childpid is %dn", childpid);
printf("PARENT: I will now wait for my child to exit.n");
wait(&status); 
printf("PARENT: Child's exit code is: %dn", WEXITSTATUS(status));
printf("PARENT: Goodbye!n"); 
exit(0); 
}
}
else 
{
printf("Fork failed due to some error");
exit(0); 
}
}

 

DOWNLOAD SOLUTION HERE
loading...
  • General_waqas

    hahahhahhha false assignemnt