473,574 Members | 13,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ to Create and Manage Unix Processes

5 New Member
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.

Expand|Select|Wrap|Line Numbers
  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>
  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;
  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.     }
  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];
  31.         for ( int i = 0; i < numCount; i++ )
  32.         {
  33.             tempNumbers[i] = numbers[i];
  34.         }
  36.         tempNumbers[numCount] = 0;
  37.         delete [] numbers;
  38.         numbers = tempNumbers;
  39.     }
  41. /*
  42.     for (int i = 0; i < numCount; i++)
  43.     {
  44.         cout << numbers[i] << endl;
  45.     }
  46. */
  48.     int result = fork();
  50.     cout << "result: " << result << endl;
  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.     }
  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 3551
5 New Member
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

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
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++. It was ported from UNIX to run under Windows using .NET. If for no other reason than as an educational exercise, I have been wondering what it...
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 active? Can the setting of LD_LIBRARY_PATH guanrantee that the correct one is executed? 2> In MS-WINDOWS, once a DLL is loaded by one...
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 behavior in order to allow intrusion detection. I was thinking of perhaps catching the SIGCHILD signal or recording exec system calls. I could probably...
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 of three processes. Each process must have a visible window as it runs. The main process is the manager. It should start the other two (player)...
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 of three processes. Each process must have a visible window as it runs. The main process is the manager. It should start the other two (player)...
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 connection, I would what the spawned processes to keep running until I shut it off... I got the os.popen method to spawn a new process running in the...
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 take it as -winpath "c:\temp\" and csh will take it literally (with the double-slashes). Is there a way for me to know what shell is currently...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.