Airport using semaphores and shared memo

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Airport using semaphores and shared memo
# 1  
Old 12-01-2010
MySQL Airport using semaphores and shared memo

Hi
just doin' this here for the naval school, back here in Pportugal, and needed some help, especially with the shared memo i want to use for the 10 airport gate, and the maximum of 4 planes preparing to leave; can´t figure out how the gate can be id by the same PID. WELL, if someone wants to know the general, the main idea is to create PIDs on a program done in unix bases, that should work in 2 modes (switch), one on automatic (just puting in the frequency of generating the planes) and the manual conf, by putting in the max of planes to generate and the frequency; only 15 planes are allowed in the air, the others are sent to an other airport;one at a time in the landing route, four gate for passagers move in, and 4 allowed to leave.
Will appreciate your help
Here is what i did 'til now:
Code:
#include "sema.h"
#define MAX_CHILD     40          /* max. # of child processes */
#define LIFETIME      30 /* max. lifetime of a process */
#define MAX_NTIME     20  /* max. time for normal work */
#define MAX_CTIME     10 /* max. time for critical work */
#define N             10
#define SHMKEY (key_t)0X10
#define SHMKEY1(key_t)0x11
int *espaco_aereo=0;
int *espaco_gate=0;
int gares=10;
main ()
{
 
 
        semaphore avioes, gate, fila, pista_mutex, ar_mutex;
        
 avioes = init_sem(15);
 gate = init_sem(10);
 fila = init_sem(4);
 pista_mutex= init_sem(1);
 ar_mutex= init_sem(1);
 
        int child_pid [MAX_CHILD], /* process ID's   */
  wait_pid;
        int i, j,   /* loop variables  */
  child_status;  /* return status of child */
 
 int shmid;
 char*addr;
 shmid=shmget(SHMKEY,10,0777|IPC_CREAT);
        addr=(char*)shmat(shmid,0,0);
        espaco_aereo=(int*)addr; 
 int shmid1;
 char*addr1;
 shmid1=shmget(SHMKEY,128,0777|IPC_CREAT);
        addr1=(char*)shmat(shmid1,0,0);
        espaco_gate=(int*)addr1; 
 
   for (i = 0; i < MAX_CHILD; ++i)
  {
    child_pid [i] = fork ();
    sleep(2);
    switch (child_pid [i])
    {
      case -1:    /* error: no process created */
 perror ("fork failed");        /*perro e igual ao printf*/
 exit (1);
 break;
      
 case 0:    /* child process  */
 
        //============== Aviões ====================
  
  P(ar_mutex); 
  if(*espaco_aereo <= 15){   
  *espaco_aereo=*espaco_aereo+1; 
  
  
  printf("O aereo TAP%d entrou no espaco aereo.\n", getpid());
  sleep(1);// ((unsigned int) rand () % MAX_NTIME);
            
          printf("existem %d avioes no ar.\n", *espaco_aereo);
  V(ar_mutex);
  P(gate);
  P(pista_mutex);
  //================ Gate ====================
  
  printf("o aereo TAP%d aterrou e entrou na gate \n", getpid());
  sleep(1);// ((unsigned int) rand () % MAX_NTIME);

  V(pista_mutex);
  P(ar_mutex);  
  *espaco_aereo=*espaco_aereo-1;
  V(ar_mutex);
  
  printf("o aereo TAP%d esta a desembarcar passageiros \n", getpid());
  
  sleep(2);
  printf("o aereo TAP%d está a embarcar passageiros \n", getpid());
  sleep(3);
  
  P(fila);
  V(gate);
  //================== fila =======================
   
   printf("o aereo TAP%d entrou na posicao da fila de espera\n", getpid());
   sleep(2);//((unsigned int) rand () % MAX_NTIME);
     
     
   V(fila);
   P(pista_mutex);
   
  //==================== Pista =========================
  printf("o aereo TAP%d descolou\n", getpid());
  
  sleep(1);// ((unsigned int) rand () % MAX_CTIME);
  V(pista_mutex);
  
  }
  
  else
      printf("o aereo %d foi desviado.\n", getpid());
      sleep(1);
      
 
 exit(0);
 break;
 
       default:    /* parent process  */
 if (i == (MAX_CHILD - 1)) /* all childs created ?  */
 {    /* yes -> wait for termination */
   for (j = 0; j < MAX_CHILD; ++j)
   {
     wait_pid = wait (&child_status); /*vai guardar o identificador do processo que terminou e desbloqueou o wait*/
     /*printf("O aviao com o numero de serie: %d aterrou e tem a matricula: %d", wait_pid, child_status);*/
     if (wait_pid == -1)
     {
       perror ("wait failed");
     };
   };
   printf ("All child processes have terminated.\n");
   
   rel_sem (avioes);
   rel_sem (gate);
   rel_sem (fila);
   rel_sem (pista_mutex);
   rel_sem (ar_mutex);
   shmdt(addr);
   shmctl(shmid,IPC_RMID,NULL);
   shmdt(addr1);
   shmctl(shmid1,IPC_RMID,NULL);
 };
    };     /* end switch   */
 };     /* end for*/
}

Joao Turbulento,Portuguese Naval school,Lisbon, Portugal

Tks

Moderator's Comments:
Mod Comment edit by bakunin: Please use CODE-tags when posting code, terminal output or similar. Thank you.

Last edited by bakunin; 12-02-2010 at 07:45 AM..
# 2  
Old 12-02-2010
Shared memeory and semaphores are so tricky and root-oriented, I like to make my own using mmap(). No root calls needed. No collisions between users on a scarece resource. No reboot to adjust. A hard copy of the shared memory if the app crashes, visible to any file tool.

Shouldn't the project be in multiple processes, like one per plane, communicating through the IPCs and taking/releasing modeled resources using it? Good Luck.

PS: You are not using the template, expect to be dinged.
# 3  
Old 12-02-2010
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Linux

HOW CAN I PRINT OUT THE MEMO ONLY?

I tried to print out external memo in LINUX system, however, each time in the print out also print out M--. M--. in front of the external memo, my question is HOW CAN I PRINT OUT THE MEMO ONLY? THANKS (0 Replies)
Discussion started by: scottblee
0 Replies

2. OS X (Apple)

Airport ID instead of machine name in Terminal

Hi. When I open a Terminal window instead of getting the machine's name (i.e. Joe-Bloggs-laptop) I get `unknown-00-1x-5x-cx-ex-ex:~ bloggs$' which is the Mac's Airport ID (ID munged). I'm on a wireless LAN with one other laptop & three iPhones (Macs running 10.6.8 & iPhones all up to date). ... (9 Replies)
Discussion started by: phildobbin
9 Replies

3. Shell Programming and Scripting

MEMO: Issue with a for loop

Can someone help me what i am doing wrong with this: apsrvrport1=28548 stcmsport1=28507 apsrvrport2=38548 stcmsport2=38507 apsrvrport3=48548 stcmsport3=48507 apsrvrport4=58548 stcmsport4=58507 apsrvrport5=38048 stcmsport5=38007 apsrvrport6=27548 stcmsport6=27507 apsrvrport7=37548... (6 Replies)
Discussion started by: mrn6430
6 Replies

4. Programming

Airport using semaphores and shared memo

Hi just doin' this here for the naval school, back here in Pportugal, and needed some help, especially with the shared memo i want to use for the 10 airport gate, and the maximum of 4 planes preparing to leave; can´t figure out how the gate can be id by the same PID. WELL, if someone wants to... (1 Reply)
Discussion started by: Turbo
1 Replies

5. Filesystems, Disks and Memory

semaphores and shared memory

hello, Can any one please answer these 2 questions? 1. What is the purpose of SEMAPHORES and SHARED MEMORY on a Unix platform? 2. What would require a SYS. ADMIN to set SEMAPHORES and SHARED MEMORY on a Unix platform? Thanks, Ted (1 Reply)
Discussion started by: ted
1 Replies
Login or Register to Ask a Question