LinuxQuestions.org
Visit Jeremy's Blog.
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 03-24-2004, 10:21 PM   #1
indika7777
LQ Newbie
 
Registered: Mar 2004
Posts: 4

Rep: Reputation: 0
Question 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
 
Old 03-24-2004, 10:52 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
check out: 'man fork' 'man waitpid' 'man shmget' 'man ipc'
 
Old 03-24-2004, 11:02 PM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
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

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.
 
Old 03-24-2004, 11:27 PM   #4
maniac01
LQ Newbie
 
Registered: Mar 2004
Posts: 11

Rep: Reputation: 0
http://www.advancedlinuxprogramming.com/alp-folder

You get some basic in this free tutorial
 
  


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
Linux Processes? cdegroat Linux - General 6 04-18-2005 01:06 PM
Many processes are starting in Linux r_213 Linux - Networking 6 11-18-2004 01:22 PM
What processes the Linux commands? Hero Doug Programming 3 10-30-2004 06:35 AM
monitoring active processes and identifying the required processes. gajaykrishnan Programming 2 08-13-2004 01:58 AM
how to stop processes in linux adhiraj Linux - General 1 02-01-2004 11:51 AM


All times are GMT -5. The time now is 12:55 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