Help (OS homework assignment in Unix)

Help (OS homework assignment in Unix)

Post by jcp121 » Thu, 05 Feb 2004 08:04:29

This is problem:

The goal of this homework is to get familiar with system calls related
to processes in the UNIX operating system. You will write a program
that uses multiple processes to compute the sum of a set of (small)
positive integers. This is a very strange method of computing the sum
of a set of numbers, but we will use it to learn about processes.

There are two kinds of processes for this assignment:

A set of ``worker'' processes. Each worker process reads two small
integers from its argv, computes its sum and returns the result using
exit system call. So, for every sum a worker process is created.

A ``coordinator'' process. It is responsible for creating the
``worker'' processes, and coordinating the computation. Note that all
the computation is done by the ``worker'' processes. All the numbers
are provided in the command line (argv).

The coordinator process also sets a timer at the start of computation
to a reasonable limit (say 10 seconds). If computation has not
finished by that time, the coordinator process kills all the workers
and then terminates. Make sure that you print appropriate message(s)
in such case.

In addition, the coordinator process should print a message when an
interrupt signal (^C) is received. However, it should not be
terminated. The worker processes just ignore the interrupt signals.
Note that the processes must handle multiple interrupts correctly. As
a precaution, add this feature only after your program is well

Note that the coordinator may have to create multiple sets of
processes. For example, if there are 8 numbers, the coordinator will
first create 4 workers and get the results from them. At this point
there are 4 numbers, and it creates 2 workers. Finally one worker is
created to compute the overall sum. To make it simpler, if the number
of integers to add is odd, the coordinator adds a 0 to the list of
numbers. Note that this may happen at any step during the computation.

Use the Sun Workstations of the Computer Science Department to work on
your programming assignments. Create a subdirectory called csci4630 in
your home directory. Create a subdirectory called assign1 in your
csci4630 directory. Use that subdirectory to store all the files
concerning this assignment and nothing else. You need to follow these
general guidelines for all your future assignments as well. Name the
two source files worker.c and coordinator.c. The code for worker
process should be compiled separately and its executable be called
worker. The executable for the coordinator process should be called
coordinator. So, to compute the sum of the numbers , the command line
would look something like:

coordinator 1 2 3 4 5 6 7

Since the results are passed around by exit keep the numbers small
(single digit). Note that this is not a good way for communication
between processes. Each worker process should print its process id,
its operands and their sum. Each time the coordinator gets a result
from a worker, it must print the pid of the worker, and the partial
sum. Achieve maximum ``parallelism'' in your implementation. If you
are not using makefile, please include the name of the compiler you
are using and any special options needed as comments (along with other
traditional comments) at the beginning of your source code.

Any help with this would really be nice.

Help (OS homework assignment in Unix)

Post by Eric Sosma » Thu, 05 Feb 2004 08:19:08

What problems have you run into in your attempts thus far?
I can't help if I don't know what difficulties you're having.



Help (OS homework assignment in Unix)

Post by jcp121 » Thu, 05 Feb 2004 13:53:25

My specific problem is the coordinator process, im having a really
hard time getting started on in it. I know my starting point will be
the int main(argc stuff but from there it is a blurr. I understand
that i will be creating child process from this process but how do i
go about doing it
If that makes it any clearer about my problem, sorry about the miss
understanding before this is my first time posting really on a board.

Thanks for any help you can give

Help (OS homework assignment in Unix)

Post by Fletcher G » Thu, 05 Feb 2004 14:39:56

Try looking at the man pages for fork(2) and exec(2) and wait(2).

Fletcher Glenn

Help (OS homework assignment in Unix)

Post by jcp121 » Fri, 06 Feb 2004 00:10:09

Yes I have a text book and I have read it, I know that i will be
using the fork command to create the child process. When I say I dont
understand how to get it started I mean that what are the steps i need
to take to get the process going. I know I will read in the data from
the command line(this will be in the Coordinator process) from there i
will do a fork command to create a child process, the child process
will call the work.c file to the computations of the two numbers given
by the coordinator. My problem is writing the code for the this, I
have the concept in my head but I dont know how to implement it. I'm
not trying to get you to do it for, that would be no good for me, I am
asking for help, because i know if I get a little help in this manner
I will be able to finish the rest on my on. Thank You

Help (OS homework assignment in Unix)

Post by Eric Sosma » Fri, 06 Feb 2004 00:48:02

Here's one way the coordinator could operate:

- First, retrieve the numbers to be summed from the
command line and stash them in a data structure of
some kind -- an ordinary array is probably good enough.

- If there are two or more numbers in the data structure,
take two of them out and launch a worker process to
add them. Whenever a worker process terminates, put
its single value back into the data structure. Keep
repeating this step until the data structure holds only
one number and all launched workers have finished (that
is, don't stop when you've got one number remaining but
there are still a couple of workers running who will
deliver other numbers "soon").

- Now the single remaining number is the sum of all the

Note that the algorithm here is slightly different from that
suggested in the assignment, in that it avoids all those artificial
zeroes. (Indeed, a nit-picky reading of the assignment suggests
that the program should never terminate: when it gets down to just
one number, the final sum, it should notice that an odd number of
values remains. Faced with an odd number of values, it should
create a zero and launch a worker to add the zero to the sum,
which once again produces an odd number of values, ...)


Help (OS homework assignment in Unix)

Post by Pascal Bou » Fri, 06 Feb 2004 01:17:06

XXXX@XXXXX.COM (Josh Parker) writes:

You have to understand two things with unix processes:

- fork makes a clone of the calling process.

- launching a new program involves two system calls: fork and exec.

fork makes a clone of the calling process. The the ONLY difference
between almost all the state (remember: all the memory and all the
register, plus the process data kept by the kernel) of the "parent"
process and that of the "child" process is the return value of the
fork call (stored in a register). (There are some kernel informations
about the processes that will differ, mainly of course the process ID,
but not much).

int result=fork();
case -1:
/* we are in the parent process,
fork could not work,
there is no child,
check errno. */
case 0:
/* we are in the child process,
we have a new pid (getpid()),
everything else is equal to the parent process.
/* Here we should do something interesting in the child,
then, most probably: */
/* we are in the parent process,
fork created a new child process whose PID is in 'result'.
/* Here we should be a good parent, doing parent stuff, and
sometimes in the future take care of the body of the child
with a call to waitpid(2) or to wait(2), to avoid
them becoming zombies. */

If the code of your child process is stored in another program, you
need to call exec(2) (or one of the other exec* functions) to load
this other program in place of the current one in the child process
(after case 0:).

You don't call a source file such as work.c from the child process.
Either you've compiled this source and obtained a separate executable
program (then you'll use exec(2) to fetch and run this program in the
child process), or you've compiled it and obtained an object file
work.o that you have linked into your main program. In that case you
already have the working code in the parent process, and then in the
child process after the fork, and you only need to call the working
_functions_ from the child process.

You have to do some things before the fork: you must decide how the
parent and its children will communicate (pipes, sockets, shared
memory, etc), and set up the communication medium.

There is no worse tyranny than to force a man to pay for what he doesn't
want merely because you think it would be good for him.--Robert Heinlein

Help (OS homework assignment in Unix)

Post by jcp121 » Fri, 06 Feb 2004 10:40:08

ascal Bourguignon < XXXX@XXXXX.COM > wrote in message news:< XXXX@XXXXX.COM >...

Thank you for the help that is exactly what i am looking for, next
question on the exec is this the write syntax: exec1("work", "work",
argv[a]. argv[a+1]);

"work" being the worker process. A wait command should follow this to
retrieve the answer from the exit command in the worker process,