473,383 Members | 2,005 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,383 software developers and data experts.

problem with argv / argc

Hello, I am working on an assignment for my 'Operating Systems' class. Our first assignment deals with creating processes using the fork(). I have most of the program already written (I can PM everything I have written if it helps) but I am getting stuck on trying to add a 0 to the end of the argv[ ] array. Whenever I use an even number on the command line like "master 1 2 3 4" it works fine because there is an even # of values to add but when there is an odd # is when I am getting segmentation faults (like "master 1 2 3").

Here is the part of my code I believe is causing problem:
Expand|Select|Wrap|Line Numbers
  1.     // Trying to add a 0 to the array if theres an odd # of values
  2.     // But its core dumping when it goes through this statement
  3.       if(argc % 2 == 0)
  4.       {
  5.     argc = argc + 1;
  6.     argv[argc] = argv[argc-1];
  7.     sprintf(argv[argc-1], "%d", 0);
  8.       }
  9.  
Heres the assignment I am working on:
You will write a program that uses multiple processes to compute the sum of a set of (small) positive integers.

There are two types of processes for this homework:

I). A set of "slaves" processes: Each slave process gets two small integers from its argv, computes its sum and returns the result using the exit system call. So, a slave process is created for every sum.

II) A "master" process: This process is responsible for creating the slave processes, and coordinating the computation. Note that all the computation is done by the "slave" processes. All the numbers are provided in the command line argv. The master process also set a timer at the start of computation to 3 seconds. If the computation has not been finished by this time, the master process kills all the slaves and then exits. You should print an appropriate message(s) in this case. Note that the master process may have to create multiple sets of slave processes. For example, if there are 8 numbers to be added, then the master process will first create 4 slaves and get the result from them. At this point there are 4 numbers, and it creates 2.

slaves. Finally one slave is created to compute the overall sum. To make it simpler, if the number of integers to add is odd, the master adds a 0 to the list of numbers. This may happen at any step during the computation. The code for master process should be compiled separately and its executable code should be called master. The executable code for the slave process should be called slave. So, to compute the sum of the numbers 1 through 7, the command line will look like

master 1 2 3 4 5 6 7

Since the results are passed around by exit system call, keep the numbers small (single digit). Each slave process prints its process id, its operands, and their sum. Each time the master gets a result from a slave, it prints the pid of the slave and the partial sum.

Hand in a hard copy and demo it on Eagle in class. (sample runs).
Oct 8 '07 #1
4 4258
Motoma
3,237 Expert 2GB
Hello, I am working on an assignment for my 'Operating Systems' class. Our first assignment deals with creating processes using the fork(). I have most of the program already written (I can PM everything I have written if it helps) but I am getting stuck on trying to add a 0 to the end of the argv[ ] array. Whenever I use an even number on the command line like "master 1 2 3 4" it works fine because there is an even # of values to add but when there is an odd # is when I am getting segmentation faults (like "master 1 2 3").

Here is the part of my code I believe is causing problem:
Expand|Select|Wrap|Line Numbers
  1.     // Trying to add a 0 to the array if theres an odd # of values
  2.     // But its core dumping when it goes through this statement
  3.       if(argc % 2 == 0)
  4.       {
  5.     argc = argc + 1;
  6.     argv[argc] = argv[argc-1];
  7.     sprintf(argv[argc-1], "%d", 0);
  8.       }
  9.  
Heres the assignment I am working on:
You are writing to a segment of memory that has not necessarily been allocated to your program. You are quire likely overwriting necessary data, you are instigating a buffer overflow with this code.
Rather than appending to the fixed length buffer argv, why not create your own copy of the data, which has a 0 appended if needed.
Oct 9 '07 #2
Motoma
3,237 Expert 2GB
I have moved this thread to the C/C++ forum, because this question is not related to Linux.
Oct 9 '07 #3
Hi,
I need to do the same program now.Can u please send me the complete program.It will be a great help.
Sep 29 '08 #4
JosAH
11,448 Expert 8TB
Hi,
I need to do the same program now.Can u please send me the complete program.It will be a great help.
I sincerely hope that the OP isn't reading nor listening to your pathetic begging.
It won't be any help because *you* need to design and implement this program,
you're not supposed to cheat. Now go and start working on it.

Jos (moderator)
Sep 29 '08 #5

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

Similar topics

2
by: Tom Jones | last post by:
Hi, I am using the .exe version of PHP and running stand alone on windows 2k ... most stuff works great but I can't read arguments off the command line. I found some references showing it...
10
by: JKop | last post by:
Why isn't: int main(int argc, char* argv) { /* ... */ } as: int main(int argc, const char* argv)
28
by: Charles Sullivan | last post by:
I'm working on a program which has a "tree" of command line arguments, i.e., myprogram level1 ]] such that there can be more than one level2 argument for each level1 argument and more than one...
19
by: hpy_awad | last post by:
What should I do to avoid the message : parameter 'argc' is never used My program is down : *-------------------- /* Book name : The prodessional programmers guide to C File name ...
13
by: Sokar | last post by:
I have my main function set up as int main(int argv, char *argv) so taht i can read in a variable which is passed to the program on the command line. The problem is that main calls other...
4
by: Xog Blog | last post by:
Hi I am by some people's standards a newbie to C, and I am refreshing my memory as to some of its conventions. I would like any patient soul out there to help out. I am having a horrible time...
5
by: mwebel | last post by:
Hi, is is also a C question but the program im writing is in C++ and it applies as well so: i have a program in the "main(int argc, const char ** argv)" and i want to make a library. for...
8
by: dragoncoder | last post by:
Hi, I am just a newbie in STL and trying to learn istream_iterator. My problem is I want a program which will take a comma separated string from the command line, tokenize it and copies into a...
11
by: vicky | last post by:
hi all, please tell me with example, how the *argv point to the the no of strings.
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.