Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Some problems here:</p> <pre><code>if (argc != 2) </code></pre> <p>This means you're expecting the integer values to be quoted, i.e. <code>a.out "1 2 3 4 5"</code>. If you do things this way the numbers are represented as a single string, i.e. <code>argv[1] := "1 2 3 4 5"</code>.</p> <p>It's easier to check for <code>argc &lt; 2</code> and taking the arguments as <code>a.out 1 2 3 4 5</code>. This way each argument gets its own string, i.e. <code>argv[1] := "1", argv[2] := "2"</code> etc.</p> <p>You can of course use a quoted list instead, but then you have add some logic to extract the integers from the string (e.g. with <code>strtok</code>) whereas argument handling can do it for you instead.</p> <p>Second, your program expects at least six integers here, and also skips the first one (you want <code>i</code> to go from <code>0</code>:</p> <pre><code>for (i = 1; i &lt;= 5; i++) sum = sum + atoi(param[i]); </code></pre> <p>As for the upper limit, one way to convey the number of integers together with their strings is to use a struct:</p> <pre><code>struct arg_struct { int argc; char **argv; }; </code></pre> <p>and then use such a struct when calling <code>pthread_create</code>, i.e.</p> <pre><code>struct arg_struct args = { argc-1, argv+1 }; pthread_create(&amp;tid,&amp;attr,(void(*)(void *))(runner),(void *)(&amp;args)); </code></pre> <p>and change <code>runner</code> accordingly:</p> <pre><code>void *runner(struct arg_struct *param) { int i; sum = 0; for (i = 0; i &lt; param-&gt;argc; i++) sum = sum + atoi(param-&gt;argv[i]); pthread_exit(0); } </code></pre> <p>Here's the code with all changes:</p> <pre><code>#include &lt;pthread.h&gt; #include &lt;stdio.h&gt; struct arg_struct { int argc; char **argv; }; int sum; /* this data is shared by the thread(s) */ void *runner(struct arg_struct *); /* threads call this function */ int main(int argc, char *argv[]) { pthread_t tid; /* the thread identifier */ pthread_t tid2; pthread_attr_t attr; /* set of thread attributes */ struct arg_struct args = { argc-1, argv+1 }; if (argc &lt; 2) { fprintf(stderr,"usage: a.out &lt;integer values&gt;\n"); return -1; } pthread_attr_init(&amp;attr); pthread_create(&amp;tid,&amp;attr,(void *(*)(void *))(runner),(void *)(&amp;args)); pthread_join(tid,NULL); printf("sum = %d\n",sum); } /* The thread will begin control in this function */ void *runner(struct arg_struct *param) { int i; sum = 0; for (i = 0; i &lt; param-&gt;argc; i++) sum = sum + atoi(param-&gt;argv[i]); pthread_exit(0); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload