473,320 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C++ to Create and Manage Unix Processes

5
Let me start by saying this for an Operating Systems class. No, I don't expect the work to be done for me.

The assignment is as follows:

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 debugged.

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.

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 result received from that worker. 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.
To be honest, I feel like a fish out of water, like many of the others in the class. We haven't been exposed to any Unix and the professor hasn't, yet, said much more than the book, which is no help at this point.

From what I've figured out and what I've blindly seen on the net and hoped would work, this is what I've come up with so far.

coordinator:
Expand|Select|Wrap|Line Numbers
  1. //COORDINATOR TEST
  2.  
  3. #include <signal.h>
  4. #include <iostream.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <sys/wait.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11.  
  12. int main( int argc, char* argv[] )
  13. {
  14.     int status = 0;
  15.     int numCount = argc - 1;
  16.     int* numbers = new int[numCount];
  17.     int numIndex = 0;
  18.  
  19.     //Place numbers into an array of their own
  20.     for ( int i = 0; i < numCount; i++ )
  21.     {
  22.         numbers[i] = strtol( argv[i+1], NULL, 10 );
  23.     }
  24.  
  25.     //If length of numbers array is odd, add a 0 to make it even
  26.     if ( ( numCount % 2 ) != 0 )
  27.     {
  28.         numCount++;
  29.         int* tempNumbers = new int[numCount];
  30.  
  31.         for ( int i = 0; i < numCount; i++ )
  32.         {
  33.             tempNumbers[i] = numbers[i];
  34.         }
  35.  
  36.         tempNumbers[numCount] = 0;
  37.         delete [] numbers;
  38.         numbers = tempNumbers;
  39.     }
  40.  
  41. /*
  42.     for (int i = 0; i < numCount; i++)
  43.     {
  44.         cout << numbers[i] << endl;
  45.     }
  46. */
  47.  
  48.     int result = fork();
  49.  
  50.     cout << "result: " << result << endl;
  51.  
  52.  
  53.     switch(result)
  54.     {
  55.         case -1: {
  56.             cout << "Unable to fork" << endl;
  57.             exit(-1);
  58.         }
  59.         case 0: {
  60.             execlp("worker", numbers[numIndex], numbers[++numIndex]);
  61.             numIndex++;
  62.             break;
  63.         }
  64.         default: {
  65.             int childPID = wait(&status);
  66.             cout << "The PID of the child process is " << childPID << endl;
  67.             cout << "The partial sum is " << ?? << endl;
  68.             break;
  69.         }
  70.     }
  71.  
  72.     return 0;
  73. }
Worker works fine on it's own. (If Anyone would like to see the code I have for that, please let me know and I will PM it)

I realize the current line:

Expand|Select|Wrap|Line Numbers
  1. execlp("worker", numbers[numIndex], numbers[++numIndex]);
won't work due to type mismatch, but no matter how I cast it there is no way I can find to make it work. Whenever I do cast it well enough for it to compile, worker doesn't like what it's getting and returns a -1.

Even if that problem is solved, I really have no idea how to proceed from there. I'm 99% sure the above isn't going to get me the solution I need. Does anyone have any suggestions?

Any help is greatly appreciated.
Feb 8 '08 #1
1 3536
jazon
5
Ok, after lots of reading, I think I've narrowed my main problems down to two questions. I believe I can take care of the rest of it, with lots of changes, after I get these taken care of.

I still have no idea how to get this to work:

Expand|Select|Wrap|Line Numbers
  1. execlp("worker", numbers[numIndex], numbers[++numIndex]);
Also, how do I get the sum from worker? The value is supposed to be passed via exit( sum ); How do I pass that back to coordinator?

Thanks again.
Feb 8 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: ajikoe | last post by:
Hello, I would like to create python processes instead of thread. Can anyone give me an example or site which shows a tutorial about how to create processes ? Sincerely Yours, Pujo
3
by: alanrn | last post by:
I would like to start a dialog on how to implement the equivalent functionality of UNIX shared memory in .NET. I work with a factory automation system. The bulk of the system is written in C/C++....
3
by: K.S.Liang | last post by:
Hi all, 1> If there are more than one dynamic linking libraries in the file system, how do I know which one is loaded into system? Any C library or system call can tell me which *.so or *.sl is...
1
by: The Boss | last post by:
Hi All, I am trying to find a way to record when processes on a Unix/Linux system are started and when (i.e. new process ID, parent process ID, spawning user,time) as a way of modeling user...
0
by: tiger | last post by:
Hi I was working on a project for school, that deals with processes and windows.....here is a sample of the instruction: You are to implement a game of Nim. Your implementation should consist...
0
by: tiger | last post by:
Hi I was working on a project for school, that deals with processes and windows.....here is a sample of the instruction: You are to implement a game of Nim. Your implementation should consist...
13
by: Godzilla | last post by:
Hello, How do you create/spawn new processes in XP over telnet using python? I.e. I would like to create a new process and have it running in the background... when I terminate the telnet...
21
by: Tom Gur | last post by:
Hi, It's seems that csh and tcsh acts a bit different when handling special characters in quotes. i.e: if i'll supply my program with the following arguments: -winpath "c:\\temp\\" tcsh will...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.