473,322 Members | 1,755 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,322 software developers and data experts.

Problem with Master/Slave program

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
Expand|Select|Wrap|Line Numbers
  1.  
  2.  #include <stdio.h>
  3.  #include <stdlib.h>
  4.  #include <sys/types.h>
  5.  #include <unistd.h>
  6.   #include <string.h>
  7. int  main(int argc, char *argv[])
  8. {
  9.      int  pid,i,q,z,x;
  10.      char  *procpath = "/home/students/fc15105/fc1510502/Slave";
  11.      char  *procname = "Slave";
  12.      int thisPid=0;
  13.      int myargc = argc-1;
  14.      int computations;
  15.      char myargs[argc];
  16.     char *mynumbers[argc];
  17.      int sumsTemp[argc];
  18.      int cc=0;
  19.      int status;
  20.      computations = myargc/2;
  21.      printf("here");
  22.      // Copy argv values into my array
  23.      for(z=0; z <= argc ;z++)
  24.     {
  25.         mynumbers[z]=argv[z+1];
  26.     }
  27.         //printf("value at argv[argc]= %s\n", argv[argc+1]);
  28.  
  29.      // Continue spawning children and passing values until the numbe of computations myargc/2 <1, meaning theres only one value left, the answer
  30.      while(computations >=1 )
  31.      {
  32.          printf("\n MY CC = %i", cc);
  33.          if(cc > 0)
  34.         {
  35.             for(x=0; x < myargc;x++)
  36.             {
  37.  
  38.                 sprintf(myargs, "%i", sumsTemp[x]);
  39.                 printf("\n %i myargs %s",x,myargs);
  40.  
  41.                 mynumbers[z]= myargs;
  42.                 printf("\n Mynumbers[%i] = %s\n",x,mynumbers[x]);
  43.             }
  44.  
  45.  
  46.         }
  47.  
  48.  
  49.         // if Number of arguments is odd, append a 0 to the array
  50.         if( myargc % 2 != 0)
  51.         {
  52.             sprintf(myargs, "%i", 0);
  53.             mynumbers[myargc]=myargs;
  54.             myargc++;
  55.             computations = myargc/2;
  56.         }
  57.  
  58.         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
  59.         {
  60.             printf("\n mynumbers[%i] : %c\n",q,*mynumbers[q]);
  61.         }
  62.  
  63.  
  64.  
  65.         printf("\n Computations : %i \n", computations); //Computations needed after check for odd argc
  66.  
  67.         for(i=0; i < computations; i++)
  68.         {
  69.             pid=fork();
  70.             thisPid=pid;
  71.  
  72.             if(pid== -1)
  73.             {
  74.                 printf("call to fork failed, no child\n");
  75.              exit(-1);
  76.             }
  77.  
  78.             if (pid == 0)
  79.             {
  80.                 execl(procpath, procname,mynumbers[2*i],mynumbers[2*i+1],0);
  81.  
  82.                 perror("execl failed to run slave program");
  83.               exit(1);
  84.             }
  85.             else if (pid >0)
  86.             {
  87.  
  88.             printf("\nParent Waiting for child # %i run throght of loop \n ",i);
  89.             wait(&status);
  90.             status = status >>8;
  91.             sumsTemp[i]=status;
  92.                     printf("\n Outside loop sumsTemp[0] = %i\n",sumsTemp[0]);
  93.         printf("\n Outside loop sumsTemp[1] = %i\n",sumsTemp[1]);
  94.         printf("\n Outside loop sumsTemp[1] = %i\n",sumsTemp[2]);
  95.         printf("\n Outside loop sumsTemp[1] = %i\n",sumsTemp[3]);
  96.  
  97.  
  98.             }
  99.  
  100.  
  101.         }
  102.  
  103.  
  104.         for(z=0;z < myargc;z++)
  105.         {
  106.  
  107.             sprintf(myargs, "%i", sumsTemp[z]);
  108.             printf("\n %i myargs %s",z,myargs);
  109.             mynumbers[z]= myargs;
  110.             printf("\n Mynumbers[%i] = %s\n",z,mynumbers[z]);
  111.         }
  112.  
  113.  
  114.         myargc=computations;
  115.         computations= myargc/2;
  116.         printf("\n ********************** END COMP ************************\n");
  117.         cc++;
  118.  
  119.      }
  120.     printf("\nSum is %i\n",sumsTemp[0]); 
  121.     for(i=0; i < argc;i++)
  122.         printf("\nmynumbers[%i]= %i\n",i,sumsTemp[i]);
  123. }
  124.  
  125.  
Oct 11 '12 #1
1 3815
WRoos
7
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
Expand|Select|Wrap|Line Numbers
  1. int* sumsTemp = malloc(argc*sizeof(int));
  2. int count = sumsTemp[index];     // use as usual
  3. free(sumsTemp)                   // when your done
  4.  
2 problem:
for(z=0; z <= argc ;z++)
should be
for(z=1; z < argc ;z++)

first arg always is programname
Oct 11 '12 #2

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

Similar topics

0
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:...
0
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...
0
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...
9
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...
3
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 *...
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...
0
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...
6
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:...
3
ram09
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...
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...
0
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...
0
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...
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...
1
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)...
1
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...
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

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.