1

Topic: unix c programming-Testing for Prime Numbers

Write a concurrent program that tests for prime numbers. The program should be able to create more than two processes. Each process should have the following functionalities.

   1.

      Each process tries to read a number n from a file named 'number.txt'. Then, the process writes n+1 back to 'number.txt' for next process.
   2.

      After fetching a number from 'number.txt', the process should test whether this number is a prime number or not. If this number is a prime number, the process will append this prime number to a file named 'prime.txt', otherwise, this number is appended to a file named 'nonprime.txt'.
   3.

      After testing 100 numbers, the process should terminate.
   4.

      If there are p processes and the starting number in 'number.txt' is s, then the numbers from s to s+100*p will be tested.



'prime.txt', 'nonprime.txt', and 'number.txt' are shared resources. The processes in your program should have mutual exclusive access to these two files. You have to test your program many times to make sure that mutual exclusion is implemented.



Your program should use the UNIX semaphore. The P(), V(), open_sem(), set_semvalue(), del_semvalue() can be found in the following program files mysem.c and mysem.h. An example of using these semaphores operations can be found at sem2.c.

PLEASE SOMEBODY HELP! I HAVE HARD TIME !!!!!!1

2

Re: unix c programming-Testing for Prime Numbers

#include "mysem.h"

#define MAXKEY 16   /* largest semahore key to try */
static int semkey = 0;   /* shared semaphore key */

/* obtain a semaphore from the system
   return a positive sem_id if success
   return -1 when failure */
int open_sem()
{
  int sem_id;

  do
    {  /* look for an available semkey */
      semkey++;
      sem_id=semget(semkey, 1, 0666|IPC_CREAT);
    }
  while ((sem_id == -1)&&(semkey < MAXKEY));

  if (semkey>=MAXKEY)
    return(-1);
  else
    return(sem_id);
}

/* set_semvalue sets the initial value of a semaphore */
/* input: sem_id is the id of the semaphore
          sem_val is the initial value of the semaphore
   output: 0 if error
           1 if success */
int set_semvalue(int sem_id, int sem_val)
{
  union semun sem_union;

  sem_union.val=sem_val;
  if (semctl(sem_id, 0, SETVAL, sem_union) == -1) return(0);
  return(1);
}

/* del_semvalue delete a semaphore */
/* input: sem_id is the id of the semaphore */
void del_semvalue(int sem_id)
{
  union semun sem_union;

  if (semctl(sem_id, 0, IPC_RMID, sem_union)==-1)
    fprintf(stderr, "Failed to delete semaphore\n");
}

/* P() operation
   input: sem_id, id of the semaphore
   output: 0 if fail, 1 if success */
int semaphore_p(int sem_id)
{
  struct sembuf sem_b;
  sem_b.sem_num = 0;
  sem_b.sem_op = -1; /* P */
  sem_b.sem_flg=SEM_UNDO;
  if (semop(sem_id, &sem_b, 1)==-1) {
    fprintf(stderr, "semaphore_p failed\n");
    return(0);
  }
  return(1);
}

/* V() operation
   input: sem_id, id of the semaphore
   output: 0 if fail, 1 if success */
int semaphore_v(int sem_id)
{
  struct sembuf sem_b;

  sem_b.sem_num=0;
  sem_b.sem_op=1; /* V */
  sem_b.sem_flg=SEM_UNDO;
  if (semop(sem_id, &sem_b, 1) == -1) {
    fprintf(stderr, "semaphore_v failed\n");
    return(0);
  }
  return(1);
}

3

Re: unix c programming-Testing for Prime Numbers

//MYSEM.H
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

int open_sem();
int set_semvalue(int sem_id, int sem_val);
void del_semvalue(int sem_id);
int semaphore_p(int sem_id);
int semaphore_v(int sem_id);

union semun {
  int val;
  struct semid_ds *buf;
  unsigned short *array;
};

4

Re: unix c programming-Testing for Prime Numbers

SOMEBODY PLZ WRITE MAIN FUNCTION FOR ME, AS I AM STILL LEARNING C PROGRAM, ABOVE IS THE MYSEM.C AND MYSEM.H

Re: unix c programming-Testing for Prime Numbers

Please go and ask your TA for assistance? This is not a homework forum...