Fork, Exec, and Signal Process Communication in CSI 4337 Programming Assignment, Assignments of Operating Systems

A programming assignment for csi 4337 where students are required to write separate programs for three processes representing animals - arnold (leader), skunky, and froggy. Arnold creates the other two processes using fork and execv, and uses signals (sigusr1 and sigusr2) to communicate with them. Each process must wait for signals from the other processes and intercept them using signal handlers. Once all signals have been received, the processes print final messages and quit.

Typology: Assignments

Pre 2010

Uploaded on 08/19/2009

koofers-user-bzd
koofers-user-bzd 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSI 4337 Programming Assignment 1 Due Feb 10, 2004
Fork, Exec, Signal
Page 1
We are going to simulate a set of three friends who go for a walk, and get lost from one
another. The three friends are a skunk called Skunky, a frog called Froggy, and a rabbit
called Arnold. Each animal will be represented by a process, and you will need to write a
separate program for each process. Arnold is the leader, so he will create the other two
processes. The Arnold process will first call fork to create the processes, and then the new
process will call execv to execute the new program. You use the return value from the
fork function to determine whether you are the Arnold process or the new process, like
this:
int x = fork( );
if (x = = 0)
{
// I am the new process
}
else
{
// I am the Arnold process
}
For the Arnold process, the return value from fork is the process ID of the new process.
You need to keep track of this so you can use it to signal the child processes. Eventually,
you will need to send a signal to the Skunky process using the following system call.
kill(Skunky_Process_ID, SIGUSR1)
You will signal the Froggy process using the following system call.
kill(Froggy_Process_ID, SIGUSR2)
But first, the Arnold process must wait for a SIGUSR1 signal from Skunky and a
SIGUSR2 signal from Froggy. Arnold will wait using the pause function. Skunky and
Froggy will find out Arnold’s process ID by executing the getppid function. They will
wander around for a while, and then signal Arnold. After signaling Arnold, they will
pause and await a reply. To simulate wandering around, generate a random number from
1 billion to ten billion and loop that many times. (Crude, but it will work.) Then use the
kill function to signal Arnold.
To intercept and process the signal, use the signal system call. Define your handler
function as follows:
void *Handler(int x)
Then call this for SIGUSR1, SIGUSR2, or both. (Arnold will need two handlers.)
sighandler_t signal(SIGUSR1,Handler)
pf2

Partial preview of the text

Download Fork, Exec, and Signal Process Communication in CSI 4337 Programming Assignment and more Assignments Operating Systems in PDF only on Docsity!

CSI 4337 Programming Assignment 1 Due Feb 10, 2004 Fork, Exec, Signal

Page 1

We are going to simulate a set of three friends who go for a walk, and get lost from one another. The three friends are a skunk called Skunky, a frog called Froggy, and a rabbit called Arnold. Each animal will be represented by a process, and you will need to write a separate program for each process. Arnold is the leader, so he will create the other two processes. The Arnold process will first call fork to create the processes, and then the new process will call execv to execute the new program. You use the return value from the fork function to determine whether you are the Arnold process or the new process, like this:

int x = fork( ); if (x = = 0) { // I am the new process } else { // I am the Arnold process }

For the Arnold process, the return value from fork is the process ID of the new process. You need to keep track of this so you can use it to signal the child processes. Eventually, you will need to send a signal to the Skunky process using the following system call.

kill(Skunky_Process_ID, SIGUSR1)

You will signal the Froggy process using the following system call.

kill(Froggy_Process_ID, SIGUSR2)

But first, the Arnold process must wait for a SIGUSR1 signal from Skunky and a SIGUSR2 signal from Froggy. Arnold will wait using the pause function. Skunky and Froggy will find out Arnold’s process ID by executing the getppid function. They will wander around for a while, and then signal Arnold. After signaling Arnold, they will pause and await a reply. To simulate wandering around, generate a random number from 1 billion to ten billion and loop that many times. (Crude, but it will work.) Then use the kill function to signal Arnold.

To intercept and process the signal, use the signal system call. Define your handler function as follows:

void *Handler(int x)

Then call this for SIGUSR1, SIGUSR2, or both. (Arnold will need two handlers.)

sighandler_t signal(SIGUSR1,Handler)

CSI 4337 Programming Assignment 1 Due Feb 10, 2004 Fork, Exec, Signal

Page 2

When Arnold gets Skunky’s signal he prints out “I can smell you, Skunky!”. When he gets Froggy’s signal he prints out “I can hear you croaking, Froggy!” When Skunky gets Arnold’s signal he prints out “I can see your ears, Arnold!” When Froggy gets Arnold’s signal he prints out “I can see your fur, Arnold!”

Once all processes have sent their signals and printed the above messages they print a final message:

“Arnold going home now.” (change the name for the other two)

After printing the final message they quit.