CS162 MP5 - Fork Exec - Part A Purpose: Study process creation MP5A: Properties of parent and child processes A process is created in Unix by a parent process calling the fork() function to create a child process. The process creation procedure has the following outline: **Start sample code *** char procname[36]; char logname[80]; struct stat buffi, buffo, buffe; struct passwd *pw; pid_t pid0, mypid, parentpid; uid_t uid, euid; gid_t gid, egid; ino_t in_ino, out_ino, err_ino; int main(void) { pid0 = fork(); if( pid0 == 0 ) { /* This is the child process */ strcpy(procname, "Child"); getprocessdata(); exit(0); } else if ( pid0 > 0 ) { /* This is the parent process */ strcpy(procname, "Parent"); getprocessdata(); exit(0); } /* else fork() error, do nothing but exit */ exit(1); } void getprocessdata(void) { /* This is your programming assignment */ } *** End sample code *** The function getprocessdata() will do the following things: 1. obtain the process's mypid and parentpid, using the functions getpid(), getppid(); 2. Get the uid and euid of the owner (user) of the process, using the functions getuid() anmd geteuid(). Further get the login name corresponding to the uid, by using the function getpwuid(). 3. Similarly, get the gid and egid of the owner of the process, using the functions getgid() and getegid(); 4. Get the inode numbers of the files assigned to the standard file descriptors STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO for the processes (parent or child), using the function fstat(). If you can, get the actual name of the device or file assigned to those inode numbers. 5. Print the address of the main() function and the address of the first global variable "char procname[]". You can print their addresses using the code: printf("%s: code segment: %d\n", procname, &main()); printf("%s: data segment" %d\n", procname, &procname[0]); Write a C program "forkexec.c" that reports all of these values found by getprocessdata(), printing each line prefixed with procname, as in: printf("%s: pid = %d\n", procname, mypid); ************************************************************** ************************************************************** CS162 MP5 - Fork Exec - Part B Purpose: Study process creation MP5B: Parent forks and waits for child Write a C program in which the parent process forks a child, then waits for the child to finish, like the following code skeleton: int main(void) { pid = fork(); if( pid == 0 ) { /* This is the child process: open for writing thge file "/tmp/timestamp" printf("Child: file /tmp/timestamp created\n"); do the following loop 60 times: sleep(1); -- do nothing for 1 second print date and time to the file "/tmp/timestamp" using the function localtime() end loop close the file "/tmp/timestamp" printf("Child: file /tmp/timestamp closed\n"); */ exit(0); } else if ( pid > 0 ) { /* This is the parent process parent waits for child to finish using the waitpid() function */ printf("Parent: child finished with return value: %d\n", childreturnvalue); exit(0); } /* else fork() error, do nothing but exit */ exit(1); } ********************************************************* ********************************************************* CS162 MP5 - Fork Exec - Part C Purpose: Study process creation MP5C: Parent forks twice, children will exec() two different programs. Write a C program in which the parent process forks two children. First child runs the program "xclock", using one of the exec*() functions. Second child runs the program "xeyes", again using an exec*() function. After the parent produces the two children, the parent exit()s, making the children orphans, and therefore, children of the init process. ******************************************************* ******************************************************* Deadline: 4:30 PM Monday, January 11, 2010. Submission format: Create a directory lastnameFirstname-mp5forkexec/ and inside this directory, put all three C programs. If you want, you can also create a README that describes how your programs differ from the ones specified here, if unable to follow the specifications given here. Then zip the directory to get lastnameFirstname-m55forkexec.zip and submit this as email attachment to prmanalastas@gmail.com |