title: Multithreading vs Multiprocessing in C
body: <p>I'm trying to learn multiprocessing and how it works compared to multithreading. I'm having a hard time finding a good source of its utilization and was wondering if anyone could give me some examples in comparison to multithreading.</p>
  
  <p>For practice, I figured I'd try to write a code with threads, and then with processes. What I'm stuck on is figure out how to create the processes I want, close the processes, and use mutex to lock/unlock process.</p>
  
  <p>For example, if I wanted to create 5 threads in a function, I can do this:</p>
  
  <pre><code>for(i = 0; i &lt; 5; i++) {
      pthread_create(&amp;(myThread[i]), NULL, myFunction, argument); 
  }
  </code></pre>
  
  <p>How would I do this in a process? I've considered calling a function and then calling fork in the function, but I'm not sure how to get a specific number. For example if I did this:</p>
  
  <pre><code>myFunction(argument) {
      fork();
      fork();
      fork();
      ...
  }
  </code></pre>
  
  <p>That would give me 8 total process. Too many. But if I removed a fork(), I'd have 4 total processes, which is too few. How would I create exactly as many process as I want?</p>
  
  <p>Next, if I were to close the threads, I'd just do this:</p>
  
  <pre><code>for(i = 0; i &lt; 5; i++){
      pthread_join(myThread[i],NULL);
  }
  </code></pre>
  
  <p>But how would I do that for processes?</p>
  
  <p>Finally, to keep threads safe, I can use mutex and call lock unlock when needed.</p>
  
  <pre><code>static pthread_mutex_t my_mutex;
  pthread_mutex_lock(&amp;my_mutex);
  ...
  pthread_mutex_unlock(&amp;my_mutex);
  </code></pre>
  
  <p>Could I use mutexes with process? If not, what alternative is there? If so, how would its implementation be different?</p>