Pages

25 Apr 2012

Cs604 2nd assignment solution spring 2012


In Linux Ubuntu flavor, write a program in C language to do the following tasks.
1) Use fork ( ) system call to create a child process.
2) Child process should first take student name as input and then it should display student name that it takes
from user. At last, child process should display numbers from 0 to 5.
3) Parent process should first take VU Student ID as input and then it should display VU Student ID that it
takes from user. At last, parent process should display numbers from 6 to 10.
4) Output of the program should be like the one give below.

Enter Student Name: Mansoor Anwar
Student Name is: Mansoor Anwar
child: 0 child: 1 child: 2  child: 3 child: 4  child: 5
Enter Student ID: BC070651121
Student ID is: BC070651121
parent: 6  parent: 7  parent: 8  parent: 9  parent: 10
5) Execute this program on 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.
The synopsis of the fork system call is as follows:
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *stat_loc);
#include <stdio.h>
void main(){
int pid, status;
pid = fork();
if(pid == -1) {
printf(“fork failed\n”);
exit(1);
}
if(pid == 0) { /* Child */
printf(“Child here!\n”);
exit(0);
}
else { /* Parent */
wait(&status);
40
printf(“Well done kid!\n”);
exit(0);
}
}

0 comments:

Post a Comment