Friday, 8 November 2013

C program to demonstrate Client/Server Inter Process Communication using Shared Memory

 [singh@00-13-02-56-15-7c shared_memory]$ vi server.c

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

#define MAXSIZE     27

void die(char *s)
{
    perror(s);
    exit(1);
}

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

    key = 5678;

    if ((shmid = shmget(key, MAXSIZE, IPC_CREAT | 0666)) < 0)
        die("shmget");

    if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
        die("shmat");

    memcpy(shm,"Rajneesh Kumar Singh",25);   


    s = shm;

      s+=25;

    *s=0;

    /*
     * Wait until the other process
     * changes the first character of our memory
     * to '*', indicating that it has read what
     * we put there.
     */
    while (*shm != '*')
        sleep(1);

    exit(0);
}



[singh@00-13-02-56-15-7c shared_memory]$ vi client.c

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE     27

void die(char *s)
{
    perror(s);
    exit(1);
}

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

    key = 5678;

    if ((shmid = shmget(key, MAXSIZE, 0666)) < 0)
        die("shmget");

    if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
        die("shmat");

    //Now read what the server put in the memory.
    for (s = shm; *s != '\0'; s++)
        putchar(*s);
    putchar('\n');

    /*
     *Change the first character of the
     *segment to '*', indicating we have read
     *the segment.
     */
    *shm = '*';

    exit(0);
}



Output :

Note: Use two terminals to generate output for this program.

In 1st Terminal :-
 
[singh@00-13-02-56-15-7c shared_memory]$ cc server.c -o server
[singh@00-13-02-56-15-7c shared_memory]$
./server


In 2nd Terminal :-

[singh@00-13-02-56-15-7c shared_memory]$ cc client.c -o client
[singh@00-13-02-56-15-7c shared_memory]$
./client
Rajneesh Kumar Singh


 

C program to create a Orphan process.

[singh@00-13-02-56-15-7c programs]$ vi orphan.c


#include <stdio.h>
void main()
{
    int pid;
    pid = fork();
    if(pid==0)
    {
        printf("I am a child");
        printf("\nChild pricessID is: %d\n Child parentID is: %d",getpid(),getppid());
        sleep(10);
        printf("\nChild processID is: %d\n Child parentID is: %d",getpid(),getppid());
    }
    else
    {
        printf("\nI am parent");
        printf("\nParent processID is: %d\n Parent parentID is: %d\n",getpid(),getppid());
    }
}
     
   
Output :

[singh@00-13-02-56-15-7c programs]$ gcc orphan.c
[singh@00-13-02-56-15-7c programs]$ ./a.out
I am a child
Child pricessID is: 9395
I am parent
Parent processID is: 9394
 Parent parentID is: 8845
[singh@00-13-02-56-15-7c programs]$  Child parentID is: 9394
Child processID is: 9395
 Child parentID is: 1

C program to create a Zombie process with processID status

[singh@00-13-02-56-15-7c programs]$ vi zombie.c


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void main()
{
    int id;
    id=fork();
    if(id>0)
    {
        printf("\nParent will sleep");
        sleep(50);
        execlp("ps","ps",(char *)0);
    }
    if(id==0)
    {
        printf("I am child\n");
        exit(10);
    }

 }

Output :

[singh@00-13-02-56-15-7c programs]$ gcc zombie.c
[singh@00-13-02-56-15-7c programs]$ ./a.out
I am child
  PID TTY          TIME CMD
 8845 pts/0    00:00:00 bash
 9025 pts/0    00:00:00 ps
 9026 pts/0    00:00:00 a.out

C program to print numbers from 1 to 10 with time interval of 1 second using alarm and signal system calls.

[singh@00-13-02-56-15-7c programs]$ vi raj20.c


#include <stdio.h>
#include <signal.h>
int i=1;

void handle(int sig)
{
        printf("%d\n",i);
        i++;
        alarm(1);
}

void main()
{
        signal(SIGALRM,handle);
        alarm(1);
        while(i<11)
        {
                sleep(1);
        }
}
Output :

[singh@00-13-02-56-15-7c programs]$ gcc -o raj20 raj20.c
[singh@00-13-02-56-15-7c programs]$ ./raj20
1
2
3
4
5
6
7
8
9
10

C program to create message queue and write 3 messages with different priorities and finally displays ( Client/Server)

[singh@00-13-02-56-15-7c message]$ vi server.c

#include <fcntl.h>
#include <sys/stat.h>       
#include <mqueue.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{

    mqd_t q1;
    char *buf1="Rajneesh";
    char *buf2="Kumar";
    char *buf3="Singh";
   
    q1 = mq_open("/test_q",O_CREAT|O_RDWR,0666,NULL);
    if(q1 == -1)
    {
         printf("Error");
    }

    mq_send(q1,buf1,strlen(buf1),1);
    mq_send(q1,buf2,strlen(buf2),2);
    mq_send(q1,buf3,strlen(buf3),3);
   
    exit(0);
}


[singh@00-13-02-56-15-7c message]$ vi client.c

#include "fcntl.h"         
#include "sys/stat.h"      
#include "mqueue.h"
#include <stdlib.h>
#include <stdio.h>
void main()
{

    mqd_t q2;
    char *buf;
    struct mq_attr *attr1;
    int prio;
    attr1 = malloc(sizeof(struct mq_attr));

    q2 = mq_open("/test_q",O_RDWR);

    if(q2 == -1)
    {
        printf("Error");
    }

    buf = malloc(10*sizeof(char));
    mq_getattr(q2, attr1);

    //In mq_receive, q2 is message queue, buf is buffer size which is pointed by attr1 and prio is the priority.
   
    mq_receive(q2,buf,attr1->mq_msgsize,&prio);   
    printf("Priority= %d",prio);
    printf("\n Message = %s\n",buf);

    mq_receive(q2,buf,attr1->mq_msgsize,&prio);
    printf("Priority= %d",prio);
    printf("\n Message = %s\n",buf);

    mq_receive(q2,buf,attr1->mq_msgsize,&prio);
    printf("Priority= %d",prio);
    printf("\n Message = %s\n",buf);

    mq_receive(q2,buf,attr1->mq_msgsize,&prio);
    printf("Priority= %d",prio);
    printf("\n Message = %s\n",buf);

    exit(0);
}
Output :
[singh@00-13-02-56-15-7c message]$ cc -lrt server.c -o server
[singh@00-13-02-56-15-7c message]$
cc -lrt client.c -o client

[singh@00-13-02-56-15-7c message]$ ./server
[singh@00-13-02-56-15-7c message]$
./client
Priority= 3
 Message = Singh
Priority= 2
 Message = Kumar
Priority= 1
 Message = Rajneesh

 

Thursday, 7 November 2013

C Program for Server/Client using sockets.

[singh@00-13-02-56-15-7c programs]$ vi server.c




#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>

int main(int argc, char *argv[])
{
    int listenfd = 0, connfd = 0;
    struct sockaddr_in serv_addr;

    char sendBuff[1025];
    time_t ticks;

    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    memset(&serv_addr, '0', sizeof(serv_addr));
    memset(sendBuff, '0', sizeof(sendBuff));

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(5000);

    bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));

    listen(listenfd, 10);

    while(1)
    {
        connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);

        ticks = time(NULL);
        snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
        write(connfd, sendBuff, strlen(sendBuff));

        close(connfd);
        sleep(1);
     }
}



[singh@00-13-02-56-15-7c programs]$ vi client.c


#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
    int sockfd = 0, n = 0;
    char recvBuff[1024];
    struct sockaddr_in serv_addr;

    if(argc != 2)
    {
        printf("\n Usage: %s <ip of server> \n",argv[0]);
        return 1;
    }

    memset(recvBuff, '0',sizeof(recvBuff));
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("\n Error : Could not create socket \n");
        return 1;
    }

    memset(&serv_addr, '0', sizeof(serv_addr));

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(5000);

    if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
    {
        printf("\n inet_pton error occured\n");
        return 1;
    }

    if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
       printf("\n Error : Connect Failed \n");
       return 1;
    }

    while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
    {
        recvBuff[n] = 0;
        if(fputs(recvBuff, stdout) == EOF)
        {
            printf("\n Error : Fputs error\n");
        }
    }

    if(n < 0)
    {
        printf("\n Read error \n");
    }

    return 0;
}

Output :

[singh@00-13-02-56-15-7c programs]$ cc server.c -o server
[singh@00-13-02-56-15-7c programs]$
cc client.c -o client
[singh@00-13-02-56-15-7c programs]$
./client 127.0.0.1

 
Error : Connect Failed
 


C Program to implement 'ls' command using directory API and various System calls.

[singh@00-13-02-56-15-7c programs]$ vi raj1.c

#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <dirent.h>

int main(void)
{
    char *curr_dir = NULL;
    DIR *dp = NULL;
    struct dirent *dptr = NULL;
    unsigned int count = 0;

    curr_dir = getenv("PWD");
    if(curr_dir == NULL)
    {
        printf("\n ERROR : Could not get the working directory\n");
        return -1;
    }

    dp = opendir((const char*)curr_dir);
    if(dp == NULL)
    {
        printf("\n ERROR : Could not open the working directory\n");
        return -1;
    }

     for(count = 0; (dptr = readdir(dp)) != NULL ; count++)
    {
        printf("%s  ",dptr->d_name);
    }
    printf("\n %u", count);
    return 0;
}








Output :

[singh@00-13-02-56-15-7c programs]$ gcc raj1.c
[singh@00-13-02-56-15-7c programs]$ ./a.out

raj11.sh  employee.txt  raj1.c~  raj14.c  raj1  raj1.sh  raj10.sh  raj6.sh  raj7.sh  raj5.sh  a.out  raj15.c  raj3.sh  raj13.c   raj8.awk  test  test4.txt  test3  raj2.sh  raj1.c  raj4.sh  raj11.sh