Subject: welcome.c

From: Jonas Schlein <[email-masked]>
Date: Mon, 4 Dec 1995 12:07:45 -0500 (EST)

#include <stdio.h>
#include <stdlib.h>
#include <task.h>
#include <ulocks.h>

void welcome (char *msg);

int thread = 0;

int main (int argc, char *argv[])
{
  if (argc != 2)
     {
     fprintf (stderr, "Usage: %s num_threads\n", argv[0]);
     exit (EXIT_FAILURE);
     }

  /* Creating the threads */
  m_set_procs (atoi (argv[1]));

  /* Do the fork */
  if (m_fork (welcome, "Welcome to thread") != 0)
     {
     fprintf (stderr, "Could not m_fork() %s processes!\n", argv[1]);
     exit (EXIT_FAILURE);
     }

  /* Kill slave threads */
  m_kill_procs ();

  return (0);
}

void welcome (char *msg)
{
 int my_id = m_get_myid(); /* Get id of current thread */

 /* Busy wait until it's our turn */
 while (my_id != thread)
    ;

 /* Lock around critical I/O region */
 m_lock () ;
 printf("%s %d\n", msg, thread++);
 m_unlock () ;

 /* Wait for everyone to finish! */
 m_sync();
}