LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-20-2003, 09:04 AM   #1
cs87668
LQ Newbie
 
Registered: Jun 2003
Posts: 6

Rep: Reputation: 0
Question how to use fork and semaphore in C/C++ in Linux?


I refer to the book of Advanced Linux Programming ( http://www.advancedlinuxprogramming.com/alp-folder , Chapter 05 )

In the book. it provides some funcation (use share memory) that let user can use the semaphores easily.

The problem is that I don't know how to use the funcation in the suitable time.

the original code of mine are following:
Code:
void main() {
                                                                                
  while (0) {  //parent process never stop
    pid_t pid = fork();
    if ( pid == 0 ) {  // child process
      do_something(); // critical secation
      exit(0);
    }
                                                                                
  }
}
and the code after inserting the semaphore funcations:
Code:
void main() {
  binary_semaphore_allocation (unique_key, IPC_CREAT | IPC_EXCL | 0666);
  binary_semaphore_initialize (id);
                                                                                
  while (0) {  //parent process never stop
    pid_t pid = fork();
    if ( pid == 0 ) {  // child process
      binary_semaphore_wait(id);
      do_something(); // critical secation
      binary_semaphore_post(id);
      binary_semaphore_deallocate(id);
      exit(0);
     }
  }
                                                                                
}

and....is everything correct?


1. the binary_semaphore_allocation and binary_semaphore_initialize funcation should execute in parent or child process?

2. should child process do binary_semaphore_deallocate funcation?

3. is any other incorrect in my code?


Thank you a lot

the complete code of funcations:
Code:
/* We must define union semun ourselves.  */

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

/* Obtain a binary semaphore's ID, allocating if necessary.  */

int binary_semaphore_allocation (key_t key, int sem_flags)
{
  return semget (key, 1, sem_flags);
}

/* Deallocate a binary semaphore.  All users must have finished their
   use.  Returns -1 on failure.  */

int binary_semaphore_deallocate (int semid)
{
  union semun ignored_argument;
  return semctl (semid, 1, IPC_RMID, ignored_argument);
}

/* Initialize a binary semaphore with a value of one.  */

int binary_semaphore_initialize (int semid)
{
  union semun argument;
  unsigned short values[1];
  values[0] = 1;
  argument.array = values;
  return semctl (semid, 0, SETALL, argument);
}

/* Wait on a binary semaphore.  Block until the semaphore value is
   positive, then decrement it by one.  */

int binary_semaphore_wait (int semid)
{
  struct sembuf operations[1];
  /* Use the first (and only) semaphore.  */
  operations[0].sem_num = 0;
  /* Decrement by 1.  */
  operations[0].sem_op = -1;
  /* Permit undo'ing.  */
  operations[0].sem_flg = SEM_UNDO;
  
  return semop (semid, operations, 1);
}

/* Post to a binary semaphore: increment its value by one.  This
   returns immediately.  */

int binary_semaphore_post (int semid)
{
  struct sembuf operations[1];
  /* Use the first (and only) semaphore.  */
  operations[0].sem_num = 0;
  /* Increment by 1.  */
  operations[0].sem_op = 1;
  /* Permit undo'ing.  */
  operations[0].sem_flg = SEM_UNDO;
  
  return semop (semid, operations, 1);
}

Last edited by cs87668; 06-20-2003 at 09:11 AM.
 
Old 06-20-2003, 10:07 AM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,533

Rep: Reputation: 148Reputation: 148
My question is: do you need to fork the process? Maybe using threads whould be also good? Threads use less resources than normal processes, that's why I recommend them.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Semaphore tzzdvd Programming 1 05-24-2005 09:23 PM
semaphore beginner_84 Programming 5 08-25-2004 06:54 PM
Linux kernel module programming(Signal and semaphore) eshwar_ind Programming 1 02-16-2004 12:10 PM
semaphore init problem in linux marry_jeo Programming 0 02-25-2003 11:20 PM
Strange behaviour of fork call in Linux Uzair Programming 1 02-12-2003 01:37 PM


All times are GMT -5. The time now is 12:40 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration