LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Creating Linux Processes using C (https://www.linuxquestions.org/questions/programming-9/creating-linux-processes-using-c-162063/)

indika7777 03-24-2004 10:21 PM

Creating Linux Processes using C
 
Hey Every one,
I want to create a lift similation in Linux/Unix environment using C. It is like this.
* Theres a 3 lifts and 10 floors
* We have to use shared memory to implement this.
* theres a routine called lift_request() & it will randomly generate request as follows.
=== Request #1, DOWN, from FOOR 7 =====
=== Request #2, UP, from FLOOR 1=====
=== Request #3, UP, from FLOOR 3 =====
* also theres a lift() routine which should handle 3 lifts
* lift requests were in a FIFO que.
* there should be 1 process for lift requests and 3 processes fo each lit handling

My problem is to implementing processes using C. Because I'm new for C & linux. Can any one help me.

Indika

infamous41md 03-24-2004 10:52 PM

check out: 'man fork' 'man waitpid' 'man shmget' 'man ipc'

aluser 03-24-2004 11:02 PM

you create a new process with fork(). It's usage is like this:

Code:

pid_t pid;
pid = fork();
if (pid < 0)
    err(1, "fork failed!");
else if (pid == 0) {
    printf("hi, I'm the child process\n");
    printf("goodbye\n");
    exit(0);
} else {
    printf("I'm the parent and I just forked a child, pid %d", pid);
}

Except for the return value from fork(), the child is an exact copy of the parent. All memory appears to be copied; they don't write on eachothers variables. (Actually, it's copy-on-write, but the look is identical)

If you want, you can get information about child processes which have died through a SIGCHLD handler. If you don't want, you should probably do this:
Code:

if (signal(SIGCHLD, SIG_IGN) == SIG_ERR)
    err(EXIT_FAILURE, "signal");

That way, you don't have idle, defunct processes ("zombies") sitting around waiting for the parent to exit.

The shared memory API I've used is SysV IPC. The functions you want are shmget(), shmat(), shmdt(), and shmctl(). There are man pages for all of these, as well as ipc(5) which is worth a read as well. The API is very ugly, but there's nothing suprising about it aside from ugliness. If you have questions I can probably|maybe answer :D

You will probably want some sort of locking on your shared memory: I've used SysV semaphores for this. The functions for those are semget(), semop(), and semctl(). Again, they all have man pages and are ugly.

You could also implement this thing with threads -- one library for that is called pthread. I find fork() to be much more intuitive than the threading stuff, and the SysV stuff is pretty reasonable once you write 10 line wrapper functions to take care of error handling and such.

maniac01 03-24-2004 11:27 PM

http://www.advancedlinuxprogramming.com/alp-folder

You get some basic in this free tutorial


All times are GMT -5. The time now is 11:05 PM.