Friday, January 29, 2016

 ********************************* Commands On LINUX********************************************

1) vi - 'vi ' is the editor of LINUX Command Prompt.

           vi Filename.c

2) gedit - 'gedit' is also the editor of LINUX or Ubuntu Command Prompt.

             gedit Filename.c
in this command gedit editor open in new window but our terminal is busy for all over process we will terminate it.

To solve this problem using following command,
     
             gedit Filename.c &

3) cp - 'cp' command is use to copy one file to another file.

            cp SourceFileName.c DestinationFileName.c

4) rm - 'rm' command is use to remove file.

            rm FileName.c

5) mv - 'mv' command is use to move one file to another file.

            mv  SourceFileName.c DestinationFileName.c

6) touch - 'touch' command is use to create new empty file or change file timestamps.

            touch fileName.c

7) cat - 'cat' command is use  to concatenate or print the content from file.


           cat FileName.c

8) chmod - 'chmod' command is use to change the access permission of file.

            chmod 0xxx Filename.c

In this command x = 0-7 value.

9) clear - 'clear' command is use to clear terminal.

10) mkdir - 'mkdir' command is use to create directory.

          mkdir DirectoryName

11) date - 'date' command is use display or change the date and time

12) df - ' df ' command is use display free disk space

13) dmesg - 'dmesg' command is use to print kernel and driver message.

14) du - 'du' command is use to estimate file space usage.

15) echo - 'echo' command is use to display message on screen

16) egrep - ' egrep' command is use to search file for lines that match an extended expression.

               egrep searchvalue FileName.c

17) fg - 'fg' command is use to send job in foreground.

18) bg - 'bg' command is use to send job in background.

19) help - 'help' command is use to display a help for built in commands.

20) history - 'history' command is use to display history of using on terminal command.

21) jobs - 'jobs' command is use to list active jobs.

22) kill - 'kill' command is use to kill a process by specifying its PID.

23) killall - 'killall' command is use to kill  processes by its name.

24) logout - 'logout' command is use to exit a login shell.

25) ls - 'ls' command is use to list all files and directories in the current directory.

26) top - 'top' command is use to list all running processes.

27)  wait - 'wait' command is use to wait for a process to complete.

28) who - 'who' command is use to print all user name currently login.

29) uname -r - 'uname -r' command is use to print kernel version number and user name.
  
30)man- "man" command is use to show all information about command.

               man ls.

31) cd - "cd" command is use to change directory.

              cd /lib.

32) pwd - "pwd" command is use to print current working directory.

33) wc - "wc" command is use to print the number of newlines, words, characters/bytes in files.

             wc FileName.c

34) ls -la - prints all detail information about list in current directory.

35) passwd - "passwd" command is use to change password.

36) reboot - Reboot the system.

37) poweroff - poweroff the system.

38) top -o mem - This command show processes using most memory first.

39) ssh username@ip - Open a shell using their username and ip.
  


Thursday, January 14, 2016

                                                      Signal Handling
1) Signal handling is one of the way of Inter Process Communication in which one process can notify the other process by sending the signal but in case of signal handling we can not send actual data to the other process.

2) In case of signal handling signal is pass through the operating system and operating system will forward that signal to the receiver process.

3) Every signal is identified by unique number which is called as signal number.

4) To perform signal handling we have to use two function as : i) signal( ) and ii) kill( ).

     i) signal( ) function is use to decide the action which is to send the signal to any of the process whose pid should be pass as parameter.

    ii) kill( ) function is use to send the signal to the any process.

5) By using signal handling we can send signal to any process which can be related or not.

--------------------------------------------------------------------------------------------------------------------------
A program which creates two process as sender and receiver in which sender process send signal to receiver process.
--------------------------------------------------------------------------------------------------------------------------
Program Name : Sender.c
--------------------------------------------------------------------------------------------------------------------------

#include  <stdio.h>
#include  <sys/types.h>
#include  <signal.h>
#include  <sys/ipc.h>
#include  <sys/shm.h>
#include<stdlib.h>

int main()
{
pid_t   pid;
key_t MyKey;
int   ShmID;
pid_t *ShmPTR;
char  line[100], c;
int   i;

MyKey   = ftok(".", 's');      

ShmID   = shmget(MyKey, sizeof(pid_t), 0666);

ShmPTR  = (pid_t *) shmat(ShmID, NULL, 0);

pid     = *ShmPTR;              

shmdt(ShmPTR);                  

while (1)
{                    
printf("Want to interrupt the other guy or kill it (i or k)? ");
gets(line);
for (i = 0; !(isalpha(line[i])); i++)
;
c = line[i];
if (c == 'i' || c == 'I') {
kill(pid, SIGINT);
printf("Sent a SIGINT signal\n");
}
else if (c == 'k' || c == 'K') {
printf("About to send a SIGQUIT signal\n");
kill(pid, SIGQUIT);  
printf("Done.....\n");
exit(0);
}
else
printf("Wrong keypress (%c).  Try again\n", c);
}
return 0;
}

/*
// OutPut :

./Sender

Want to interrupt the other guy or kill it (i or k)? k
About to send a SIGQUIT signal
Done.....
*/

--------------------------------------------------------------------------------------------------------------------------

Program Name : - Receiver.c
--------------------------------------------------------------------------------------------------------------------------

#include  <stdio.h>
#include  <sys/types.h>
#include  <signal.h>
#include  <sys/ipc.h>
#include  <sys/shm.h>
#include<unistd.h>
#include<stdlib.h>

void  SIGINT_handler(int);
void  SIGQUIT_handler(int);

int   ShmID;
pid_t *ShmPTR;  

int main()
{
int   i;
pid_t pid = getpid();
key_t MyKey;

signal(SIGINT, SIGINT_handler);
signal(SIGQUIT, SIGQUIT_handler);

MyKey   = ftok(".", 's');  
ShmID   = shmget(MyKey, sizeof(pid_t), IPC_CREAT | 0666);

ShmPTR  = (pid_t *) shmat(ShmID, NULL, 0);

*ShmPTR = pid;            

for (i = 0; ; i++)
{      
printf("From process %d: %d\n", pid, i);
sleep(1);
}
return 0;
}
//////////////////////////////////////////////////////////////////////////////

void  SIGINT_handler(int sig)
{
signal(sig, SIG_IGN);
printf("From SIGINT: just got a %d (SIGINT ^C) signal\n", sig);
signal(sig, SIGINT_handler);
}

void  SIGQUIT_handler(int sig)
{
signal(sig, SIG_IGN);
printf("From SIGQUIT: just got a %d (SIGQUIT ^\\) signal"
" and is about to quit\n", sig);
shmdt(ShmPTR);
shmctl(ShmID, IPC_RMID, NULL);

exit(3);
}

/*
 OutPut :

./Receiver

From process 3720: 0
From process 3720: 1
From process 3720: 2
From process 3720: 3
From process 3720: 4
From process 3720: 5
From process 3720: 6
From process 3720: 7
From process 3720: 8
From process 3720: 9
From process 3720: 10
From SIGQUIT: just got a 3 (SIGQUIT ^\) signal and is about to quit

*/
--------------------------------------------------------------------------------------------------------------------------

Saturday, January 2, 2016

Message Queue:-

1) This is the another way of Inter Process Communication. In which one process can communicate with other process by sending the message.

2) In this concept we have to send message to the operating system and that message is accepted by the operating system then that message gets inserted into a message queue by the operating system.

3) If receiver process is running then that process request to get the message to the operating system then depend on the id of that process operating system receive that message from the message queue and that message gets displayed to the receiver process.

4) The internal mechanism of message queue and shared memory is almost similar.
------------------------------------------------------------------------------------------------------------

Steps followed by the message sender.

1) Include header file as msg.h.

2) Decide the structure which is use to receive the message.

3) Decide the key which is use for communication.

4) Call the function msgget() to start the communication by specifying the flag as IPC_CREAT.

5) Call the function msgsnd() by filling the data inside that message structure.
------------------------------------------------------------------------------------------------------------

Steps followed by message receiver.

1) Include header file msg.h.

2) Use the communication key which is decided by the sender.

3) Call function msgget() by specifying that key.

4) Call function msgrcv() and pass the address of empty structure to receive that message.

------------------------------------------------------------------------------------------------------------
/* This program illustrate that the concept of Message Queue. */
/* Program Name:- Sender.c
------------------------------------------------------------------------------------------------------------

#include<stdio.h>
#include<string.h>
#include<sys/msg.h>
#define MSGSIZE 256

struct message
{
int id;
char data[MSGSIZE];
};

int main()
{
int msgid;
key_t key;
struct message msg;

key = 9999;

/*
The  msgget()  system call returns the message queue identifier associated with the value of the key argument.
  */

msgid = msgget(key, IPC_CREAT | 0666);

msg.id = 1;

strcpy(msg.data, "Hello World");

/*  The  msgsnd() system calls are used to send messages to a message queue. The calling process     must  have write  permission on the message queue in order to send a message
*/
/* The msgsnd() system call appends a copy of the message  pointed  to  by msg to the message queue
  whose identifier is specified by msgid.
*/
msgsnd(msgid, &msg, MSGSIZE, 0);

return 0;
}

------------------------------------------------------------------------------------------------------------

/* This program illustrate that the concept of Message Queue. */
/* Program Name:- Receiver.c */

------------------------------------------------------------------------------------------------------------

#include<stdio.h>
#include<sys/msg.h>
#define MSGSIZE 256

struct message
{
int id;
char data[MSGSIZE];
};

int main()
{
int msgid;
key_t key;
struct message msg;
key = 9999;
/*
The  msgget()  system call returns the message queue identifier associated with the value of the key argument. 
  */
msgid = msgget(key, 0666);
/* The msgrcv() system calls are used to receive messages from a message queue. The calling process must have read permission to receive a message.
*/
/*
The msgrcv() system call removes a message from the queue specified by msgid and places it in the buffer pointed to by msgp.
*/
msgrcv(msgid, &msg, MSGSIZE,1, 0);
printf("%s",msg.data);
return 0;
}

------------------------------------------------------------------------------------------------------------

For compilation read previous post.............
Inter Process Communication in UNIX

   1)      System V is universally accepted and most successful version of UNIX Operating System. This version of UNIX provides multiple things to other Operating System such as
i)                    TCP/IP Protocol suite.
ii)                   Multiple IPC Techniques.
iii)                 Concept of Pipe.
iv)                 Concept of Socket Programming etc.
   2)      We can perform Inter Process Communication by using different techniques such as
i)                    Shared Memory.
ii)                   Message Queue.
iii)                 Pipe.
iv)                 Memory Map Files.
v)                  FIFO (Queue).
   3)      Each an every IPC techniques has different perspective depend on the data should be send and the data which can be received by the other processes.


1)      Shared Memory :-
i)                    Shared memory is the fastest way of process communication.
ii)                   In case of shared memory the actual memory gets allocated in address space of the sender process.
iii)                 That address space is share by the receiver process.
iv)                 That two process can communicate with each other by writing and reading the data from that shared memory.
v)                  To demonstrate the concept of shared memory there should be at least two process on same machine.
vi)                 There are some steps which are followed by the sender process as
i)                    Decide the key for communication.
ii)                   Call the function shmget()  to allocate shared memory.
iii)                 Call the function shmat() which is use to attach that shared memory to our address .
iv)                 Write the data inside that allocated memory.
v)                  Detach that allocated memory by calling shmdt()function.

       v)          Steps followed by the receiver process as
                                                               i.      Use the key which is use by the sender process for communication.
                                                             ii.      Call the function shmget() to locate that shared memory segment which is allocated by sender process.
                                                            iii.      Call the function shmat() to attach that segment.
                                                           iv.      Read the data from that shared memory segment.
                                       v.     Deallocate that memory by calling shmdt() function.
----------------------------------------------------------------------------------------------------------------------------------

/* Sender process program */

/* Program Name: Sender.c

----------------------------------------------------------------------------------------------------------------------------------

/* This program demostrate the concept of Shared Memory in Unix. */

#include<stdio.h>
#include<sys/shm.h>

#define SHMSIZE 256

int main()
{
char *shm, *s, ch;
int shmid;
key_t key;

key = 9999;

/* shmget() returns the identifier  of the shared memory segment associated with the value of the argument key.
  This function is use to allocate the shared memory.
  Parameters:
1. Required key value which known by sender and receiver.
2. Size of shared memory.
3. shared memory flag and permission.
*/   
shmid = shmget(key, SHMSIZE, IPC_CREAT | 0666);

/* shmat() attaches the shared memory segment identified by shmid to the address space of the calling process.
  Parameters:
1. Return value of shmget() function.
2. If shmaddr is NULL, the system chooses a suitable (unused) address at which to attach the segment.
3. shared memory flags.
*/
shm = shmat(shmid, NULL, 0);

s = shm;

for(ch = 'a'; ch <= 'z'; ch++)
{
*s = ch;
s++;
}
*s = NULL;

while(*shm != '*')
sleep(1);

shmdt(shm);
return 0;

}

------------------------------------------------------------------------------------------------------------
/* Receiver process program */
/* Program Name: Receiver.c

------------------------------------------------------------------------------------------------------------

/* This program demostrate the concept of Shared Memory in Unix. */

#include<stdio.h>
#include<sys/shm.h>

#define SHMSIZE 256

int main()
{
char *shm, *s, ch;
int shmid;
key_t key;
key = 9999;

/* shmget() returns the identifier  of the shared memory segment associated with the value of the argument key.
  This function is use to allocate the shared memory.
  Parameters:
1. Required key value which known by sender and receiver.
2. Size of shared memory.
3. shared memory flag and permission.
*/   
shmid = shmget(key, SHMSIZE, 0666);
/* shmat() attaches the shared memory segment identified by shmid to the address space of the calling process.
  Parameters:
1. Return value of shmget() function.
2. If shmaddr is NULL, the system chooses a suitable (unused) address at which to attach the segment.
3. shared memory flags.
*/
shm = shmat(shmid, NULL, 0);
for(s = shm; *s != NULL; s++)
{
printf("%c",*s);
}
printf("\n");
*shm = '*';
shmdt(shm);
return 0;
}
------------------------------------------------------------------------------------------------------------

Steps to compile it.
1) gcc -o sender Sender.c

2) gcc -o receiver Receiver.c

3) Open two terminal at time to see the output.

4) ./sender

5) ./receiver.

------------------------------------------------------------------------------------------------------------

SYBSc (CS) Sem III : Data Structure Slip 20 Que - 2

    SAVITIBAI PHULE UNIVERSITY OF PUNE S. Y. B.Sc. (Computer Science) Semester III Practical Examination       SUBJECT: CS-233 Practical cou...