Monday, September 22, 2008

Accessing shared memory using C in Linux

Server :


Note: Don't forget to use shmdt & shmctl to detach & de-allocate shared memory segment !!!

Implementation of race condition using C and thread in Linux

Implementation of race condition:-

Compile as : gcc -lpthread file1.c


Implementation of Zombie Process

Implementation of Zombie Process ---
Click on the image below to enlarge it..

Output ....
Click on the image below to enlarge it..

Art Auctions starting at $1

What is Zombie Process ?

On Unix operating systems, a zombie process or defunct process is a process that has completed execution but still has an entry in the process table, allowing the process that started it to read its exit status. In the term's colorful metaphor, the child process has died but has not yet been reaped.

When a process ends, all of the memory and resources associated with it are deallocated so they can be used by other processes. However, the process's entry in the process table remains. The parent is sent a SIGCHLD signal indicating that a child has died; the handler for this signal will typically execute the wait system call, which reads the exit status and removes the zombie. The zombie's process ID and entry in the process table can then be reused. However, if a parent ignores the SIGCHLD, the zombie will be left in the process table. In some situations this may be desirable, for example if the parent creates another child process it ensures that it will not be allocated the same process ID.
A zombie process is not the same as an orphan process. Orphan processes don't become zombie processes; instead, they are adopted by init (process ID 1), which waits on its children.

The term zombie process derives from the common definition of zombie, an undead person.

Zombies can be identified in the output from the Unix ps command by the presence of a "Z" in the STAT column. Zombies that exist for more than a short period of time typically indicate a bug in the parent program. As with other leaks, the presence of a few zombies isn't worrisome in itself, but may indicate a problem that would grow serious under heavier loads.

To remove zombies from a system, the SIGCHLD signal can be sent to the parent manually, using the kill command. If the parent process still refuses to reap the zombie, the next step would be to remove the parent process. When a process loses its parent, init becomes its new parent. Init periodically executes the wait system call to reap any zombies with init as parent.

Implementation of Zombie using C in Linux.

Monday, June 30, 2008

Implementation of producer-consumer problem using C in Linux

Implementation of producer-consumer problem using C in Linux

/* File name: th1.c */
/* Compile: gcc -lpthread -o th1 th1.c */
/* Run: ./th1 */
#include "pthread.h"
#include "stdio.h"

#define N 6
int c=0;
int buf[N];
int in,out;
void * f1(void *m);
int main(){
pthread_t th;
int d,flag=0;
printf("\nEnter the data : ");
} //buffer is full
return 0;
void * f1(void *m){
int d,flag=0;
printf("\nConsumed item : %d",d);

Monday, June 23, 2008

Using fork in Linux/Unix

Using fork in Linux/Unix :-

A useful way of creating processes is by using fork, but you must take care to keep track of child processes, here is an example of fork written in c.

include int files "sys/types.h", "unistd.h", and "stdio.h".

int main()
pid_t id;
char *message;
int n;
printf("fork program startingn");
id = fork();
switch(id) {
case -1:
case 0:
message = "this is the child process";
n = 3;
message = "this is the parent process";
n = 6;
for(; n > 0; n-)

save it as fork1.c and now lets compile this small fork program using the GNU C compiler, now just use the following command: cc -o fork1 fork1.c
Now you can run it : fork &