Monday, April 18, 2011
Some Good Unix Commands
1) stat file_name : will give you complete information about the file. It will also tell you the inode number.
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.
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/
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;
}
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]
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]
Monday, January 31, 2011
1 Basic of Process
Program will tell about basic of the process in linux system
/*
This program explain about the basic process creation in the linux operating system
1) its obvious that statement after the fork are executed twice. once on the behalf of main program and second as child process
*/
#include
int main()
{
int pid;
printf("\nThis will be executed only once [%d] [%d]\n", getpid(), getppid());
fork();
printf("\nTHis will be executed twice\n", getpid(), getppid());
return 0;
}
Output :
This will be executed only once [4665] [4299]
THis will be executed twice [4666] [4665]
THis will be executed twice [4665] [4299]
/*
program will show some clever use of pid. In this way you can segregate the parent process and child process code
one more important point fork returns zero to the child process.
why it return zero is, the answer is its the kernel implemenation of the kernel code
http://fxr.watson.org/fxr/source/kern/kern_fork.c?v=FREEBSD8
he code is a bit confusing if you're not used to reading kernel code, but the inline comments give a pretty good hint as to what's going on.
The most interesting part of the source with an explicit answer to your question is at the very end of the fork() definition itself -
if (error == 0) {
td->td_retval[0] = p2->p_pid;
td->td_retval[1] = 0;
}
"td" apparently holds a list of the return values for different threads. I'm not sure exactly how this mechanism works (why there are not two separate "thread" structures). If error (returned from fork1, the "real" forking function) is 0 (no error), then take the "first" (parent) thread and set its return value to p2 (the new process)'s PID. If it's the "second" thread (in p2), then set the return value to 0.
There is one more thing you can notice, only pid is set to zero not all the local variable, which tells justifies the kernel things
*/
#include
int main()
{
int pid;
int temp;
pid = fork();
if(0 == pid)
{
printf("\n I am child process [%d] [%d]\n", getpid(), getppid());
printf("Temp and Pid [%d] [%d]", temp, pid) ;
}
else
{
printf("\n I am Parent process [%d] [%d]\n", getpid(), getppid());
printf(" Temp and Pid [%d] [%d]", temp, pid) ;
}
return 0;
}
Output:
I am child process [4885] [4884]
Temp and Pid [-1080938856] [0]
I am Parent process [4884] [3314]
Temp and Pid [-1080938856] [4885]
/*
This program explain about the basic process creation in the linux operating system
1) its obvious that statement after the fork are executed twice. once on the behalf of main program and second as child process
*/
#include
int main()
{
int pid;
printf("\nThis will be executed only once [%d] [%d]\n", getpid(), getppid());
fork();
printf("\nTHis will be executed twice\n", getpid(), getppid());
return 0;
}
Output :
This will be executed only once [4665] [4299]
THis will be executed twice [4666] [4665]
THis will be executed twice [4665] [4299]
/*
program will show some clever use of pid. In this way you can segregate the parent process and child process code
one more important point fork returns zero to the child process.
why it return zero is, the answer is its the kernel implemenation of the kernel code
http://fxr.watson.org/fxr/source/kern/kern_fork.c?v=FREEBSD8
he code is a bit confusing if you're not used to reading kernel code, but the inline comments give a pretty good hint as to what's going on.
The most interesting part of the source with an explicit answer to your question is at the very end of the fork() definition itself -
if (error == 0) {
td->td_retval[0] = p2->p_pid;
td->td_retval[1] = 0;
}
"td" apparently holds a list of the return values for different threads. I'm not sure exactly how this mechanism works (why there are not two separate "thread" structures). If error (returned from fork1, the "real" forking function) is 0 (no error), then take the "first" (parent) thread and set its return value to p2 (the new process)'s PID. If it's the "second" thread (in p2), then set the return value to 0.
There is one more thing you can notice, only pid is set to zero not all the local variable, which tells justifies the kernel things
*/
#include
int main()
{
int pid;
int temp;
pid = fork();
if(0 == pid)
{
printf("\n I am child process [%d] [%d]\n", getpid(), getppid());
printf("Temp and Pid [%d] [%d]", temp, pid) ;
}
else
{
printf("\n I am Parent process [%d] [%d]\n", getpid(), getppid());
printf(" Temp and Pid [%d] [%d]", temp, pid) ;
}
return 0;
}
Output:
I am child process [4885] [4884]
Temp and Pid [-1080938856] [0]
I am Parent process [4884] [3314]
Temp and Pid [-1080938856] [4885]
Subscribe to:
Posts (Atom)