Wednesday, February 16, 2011

Message Queue

Message queues can also be used as synchronization object also.
In these kind of cases there is no payload, some bodya will just wait on the messge queue.
When message is received thread/process can continue their work.

Same thing is persent in the J Jobs. Where other J need to wait for ther other modules to complete the activity only then you can mark the job as competed. In that case J would be waiting for the message. Other module like I will send the message, then we will mark the job as competed.

In pSOS we have two types of queue, first one is the one having 4 long words as playload, Most of the embedded system these are more then eough to do the work. As we work of the physical address in single process address space.

But pSOS also has one more queue which is like same as System V IPC queue.

IPC: Message Queues

System V IPC consist of
Message Queues, Semaphores, Shared memory
Message Queues
Before talking about the what Message queues are, just like to point out disadvante of the other IPC, FIFO (named pipes)
Diadvantages of FIFO
1) data cannot be broadcased to mutliple receivers.
2) Cannot store Data
3) No message boundaries, Data is treaded as stream of bytes.

How to use
1) Create a Unique identification number, ftok() function
key_t ftok(const char *filenme, int id)
2) Get the resource
xxxget(ket_t key, int xxflg)
this syntax is common for all the ipc resources (msg, shm, sem)
3) All system V resources are controled by xxxctl function
xxxctl(int xxxid, int cmd, struct xxxid_ds *buffer)
The command argument can be anyone of these
IPC_STAT =>copy the infro the info from kernel structure to the pointer buffer
IPC_SET => write the info pointer by buffer to the message data structure
IPC_RMID => remove the object

4)
int msgget(key,msglag)
msgflog = IPC_CREATE | 0777
this 0777 is the queue permisssion
Message structure and message sending
struct my_msgbuf {
long mtype,
char any_data_arr[354]
};
Here you must should note that this "mtype" is necessary and used while receving the message.
"any_data_arr" can be any filed or you can add any number of the data elements in this structure later.

5) Send the mssage
msgsnd(msgid, &my_msbug, sizeof(my_msgbuf)-sizeof(long), flag)

flag = 0 or IPC_NOWAIT

6) Recevie the message
msgrcv(msgid, &my_msbug, sizeof(my_msgbuf)-sizeof(long), flag)
flag values are as below
0 = recevie the next message
postive = get the next message, whose mtype is equal to the flag
-ve = retrive the message whose abosolute values is less then or equal to the given value

7) When every thing is done do
msgctl(msqid, IPC_RMID, NULL)

http://beej.us/guide/bgipc/

Sunday, February 6, 2011

Zombie Process

/*
Zombie Process
Zombie means undead. Funda here is very simple,
->when process exit, its return status is collected by its parent
->IN case of orphan prcess, when child exits the parent is in sleep state and will not collect the status of the child
->as child status is uncollected, it cannot be cleared from system table and its occupies some of the system resources
->So its bad house keeping, as zombie is taking one process id etc.

*/

#include
int main()
{
int pid;

if(0==fork())
{

printf("Child Process [%d] Parent [%d]\n",getpid(), getppid());
printf("\n Check process table \n");
}
else
{
/*Parent immediatly spleeps and after child finishes you can check
process talbe there would be child process with defunct
*/
sleep(20);
printf("\n Parent waking up \n");
printf("Parent Process [%d] Parent [%d]\n",getpid(), getppid());

}

return 0;


}

Orphan Process

/*
This program will tell about orphan process.
As of now i cannot think of any side effect of orphan process
only thing which possibly could be your ophan process eating the resources which idealy you dont want it to have

*/

#include
int main()
{
int pid;

if(0==fork())
{
printf("Child Process [%d] Parent [%d]\n",getpid(), getppid());
sleep(5);
printf("Child Process [%d] Parent [%d]\n",getpid(), getppid());
}
else
{
sleep(1);
printf("Parent Process [%d] Parent [%d]\n",getpid(), getppid());
printf("Parent Process [%d] Parent [%d]\n",getpid(), getppid());
sleep(2);
}

return 0;


}

./3_orphan
Child Process [7070] Parent [7069]
Parent Process [7069] Parent [6602]
Parent Process [7069] Parent [6602]
Child Process [7070] Parent [1]