I am trying to create this program where pairs of integers are passed to a slave process that adds the two integers and returns the value to the Master. The Master continues to spawn slaves until all the numbers are summed into a single answer. If there are an odd number of arguments, add a 0 to the end to make it even. Here's my code. I'm not sure why its not working. If I enter 1 or 3 arguments it works fine, any other and it doesn't, any quick response would be really helpful, thanks -
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <sys/types.h>
-
#include <unistd.h>
-
#include <string.h>
-
int main(int argc, char *argv[])
-
{
-
int pid,i,q,z,x;
-
char *procpath = "/home/students/fc15105/fc1510502/Slave";
-
char *procname = "Slave";
-
int thisPid=0;
-
int myargc = argc-1;
-
int computations;
-
char myargs[argc];
-
char *mynumbers[argc];
-
int sumsTemp[argc];
-
int cc=0;
-
int status;
-
computations = myargc/2;
-
printf("here");
-
// Copy argv values into my array
-
for(z=0; z <= argc ;z++)
-
{
-
mynumbers[z]=argv[z+1];
-
}
-
//printf("value at argv[argc]= %s\n", argv[argc+1]);
-
-
// Continue spawning children and passing values until the numbe of computations myargc/2 <1, meaning theres only one value left, the answer
-
while(computations >=1 )
-
{
-
printf("\n MY CC = %i", cc);
-
if(cc > 0)
-
{
-
for(x=0; x < myargc;x++)
-
{
-
-
sprintf(myargs, "%i", sumsTemp[x]);
-
printf("\n %i myargs %s",x,myargs);
-
-
mynumbers[z]= myargs;
-
printf("\n Mynumbers[%i] = %s\n",x,mynumbers[x]);
-
}
-
-
-
}
-
-
-
// if Number of arguments is odd, append a 0 to the array
-
if( myargc % 2 != 0)
-
{
-
sprintf(myargs, "%i", 0);
-
mynumbers[myargc]=myargs;
-
myargc++;
-
computations = myargc/2;
-
}
-
-
for(q=0; q < argc; q++) // For some reason, on 2nd iteration through while loop, the values previously stored in the array mynumbers[] are gone
-
{
-
printf("\n mynumbers[%i] : %c\n",q,*mynumbers[q]);
-
}
-
-
-
-
printf("\n Computations : %i \n", computations); //Computations needed after check for odd argc
-
-
for(i=0; i < computations; i++)
-
{
-
pid=fork();
-
thisPid=pid;
-
-
if(pid== -1)
-
{
-
printf("call to fork failed, no child\n");
-
exit(-1);
-
}
-
-
if (pid == 0)
-
{
-
execl(procpath, procname,mynumbers[2*i],mynumbers[2*i+1],0);
-
-
perror("execl failed to run slave program");
-
exit(1);
-
}
-
else if (pid >0)
-
{
-
-
printf("\nParent Waiting for child # %i run throght of loop \n ",i);
-
wait(&status);
-
status = status >>8;
-
sumsTemp[i]=status;
-
printf("\n Outside loop sumsTemp[0] = %i\n",sumsTemp[0]);
-
printf("\n Outside loop sumsTemp[1] = %i\n",sumsTemp[1]);
-
printf("\n Outside loop sumsTemp[1] = %i\n",sumsTemp[2]);
-
printf("\n Outside loop sumsTemp[1] = %i\n",sumsTemp[3]);
-
-
-
}
-
-
-
}
-
-
-
for(z=0;z < myargc;z++)
-
{
-
-
sprintf(myargs, "%i", sumsTemp[z]);
-
printf("\n %i myargs %s",z,myargs);
-
mynumbers[z]= myargs;
-
printf("\n Mynumbers[%i] = %s\n",z,mynumbers[z]);
-
}
-
-
-
myargc=computations;
-
computations= myargc/2;
-
printf("\n ********************** END COMP ************************\n");
-
cc++;
-
-
}
-
printf("\nSum is %i\n",sumsTemp[0]);
-
for(i=0; i < argc;i++)
-
printf("\nmynumbers[%i]= %i\n",i,sumsTemp[i]);
-
}
-
-
1 3815
I have no experience with programs without forms, but I see 2 problems:
int sumsTemp[argc];
1) You can't declare tables in this way. the computer needs to know at compile-time how many memory will be used. You can solve this by defining tables for 100 int's. Always enough...
or you can use -
int* sumsTemp = malloc(argc*sizeof(int));
-
int count = sumsTemp[index]; // use as usual
-
free(sumsTemp) // when your done
-
2 problem:
for(z=0; z <= argc ;z++)
should be
for(z=1; z < argc ;z++)
first arg always is programname
Sign in to post your reply or Sign up for a free account.
Similar topics
by: jasper |
last post by:
hi,
i am trying to make a very simple program that will just detect
and output your currently connected IDE devices. It will look
something like this:
IDE PRIMARY MASTER:
IDE PRIMARY SLAVE:...
|
by: flubdgub |
last post by:
Quick question I haven't found a straight answer to in MySQLs docs...
In the case of 2 3.23.X series mysql databases in a master/slave
replication setup (myiasm tables), if a FLUSH TABLES WITH...
|
by: Winston Ng |
last post by:
Hi,
I have 2 servers (A and B) setup as Master and Slave respectively.
Upon A's failure, all client's request will be directed to B.
How can I set A to become master again after its been...
|
by: Jan Wieck |
last post by:
Dear community,
for some reason the post I sent yesterday night still did not show up on
the mailing lists. I have set up some links on the developers side under...
|
by: Suri |
last post by:
hi im trying to run this simple program. any help shall be appreciated
..pls see error message.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
double pic;
pic= 4.0 *...
|
by: stefanjohansson_2005 |
last post by:
I can databind to a relation in WindowsForms for example:
I have a typed DataSet with an "Orders" table and a "Order Details"
table. They have a relation called "OrderRelation".
I have a...
|
by: erkidevries |
last post by:
Problem compiling Tkinter program with bmp images (using py2exe)
I have a Tkinter gui program that uses bmp as backgrounds.
The program itself works when I run from the source.
I placed the...
|
by: ashishnh33 |
last post by:
cud any one tell me what is the problem in this program
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int binarysearch(void)
int linearsearch(void)
class search
{
private:...
|
by: ram09 |
last post by:
ERROR 1200 <HY000>: The sever is not configured as slave; fix in config file or with CHANGE MASTER TO... i always encounter this error whenever i start slave...i already reset my master and slave..i...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
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...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
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
| |