Sunteți pe pagina 1din 20

LAB4

1) ADD.C
#include<stdio.h>

int add()
{
int a, b, c;
printf("Enter two numbers to add\n");
scanf("%d%d",&a,&b);
c = a + b;
printf("Sum of entered numbers = %d\n",c);
return 0;
}

DIV.C
#include<stdio.h>

int div()
{
int a; int b; int c;
printf("enter the values");
scanf("%d",&a);
printf("enter the 2nd value");
scanf("%d",&b);
c=a/b;
printf("your valur after divide is: %d\n",c);
return 0 ;
}

MUL.C

#include<stdio.h>

int mul()
{
int a; int b; int c;
printf("enter the values");
scanf("%d",&a);
printf("enter the 2nd value");
scanf("%d",&b);
c=a*b;
printf("your valur after multiplication is: %d\n",c);
return 0 ;
}

SUB.C
#include<stdio.h>

int sub()
{
int a, b, c;
printf("Enter two numbers to sub\n");
scanf("%d%d",&a,&b);
c = a - b;
printf("Sub of entered numbers = %d\n",c);
return 0;
}

MAIN.H

int add();
int sub();
int mul();
int div();

CODE FOR RUNNING
to compile= gcc -o main main.c add.c sub.c mul.c div.c -I.
for out=./main

3)#include<stdio.h>

/*Program to multiply two 3*3 matrices*/

void main()
{
int a[3][3],b[3][3],c[3][3],i,j,k;
printf("Enter elements of A:");
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
scanf("%d",&a[i][j]);

printf("Enter elements of B:");
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
scanf("%d",&b[i][j]);

printf("A:");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
printf("%d ",a[i][j]);
printf(""); //To change line.
}

printf("B:");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
printf("%d ",b[i][j]);
printf(" ");
}
k=0;
while(k<=2)
{
for(i=0;i<=2;i++)
{
int sum=0;
for(j=0;j<=2;j++)
sum=sum+a[i][j]*b[j][k];
c[i][k]=sum;
}
k++;
}
printf("Result:");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
printf("%d ",c[i][j]);
printf(" ");
}
}
LAB5

1)#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{

pid_t pid;
char *message;
int n;
printf("fork starting\n");
pid = fork();

switch(pid)
{
case -1:
perror("fork failed");
exit (1);
case 0:
message = "Child";

n=7;
execl("diffprog", 0);
break;
default:
message = "Parent";
n=5;
break;
}
for(;n>0; n--) {
puts(message);
sleep(1);
}
exit(0);

}

2)#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
pid_t pid;
int n=atoi(argv[1]);
for(;n>0;n--)
{
pid = fork();
switch(pid)
{
case -1:
perror("fork failed");
exit (1);

case 0:
exit(0);
break;

default:
printf("\nChild created with id = %d\n",pid);
break;
}
}
exit(0);
return 0;
}


3)#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
pid_t pid;
char *message;
int n;
printf("fork starting\n");
pid = fork();

switch(pid)
{

case -1:
perror("fork failed");
exit (1);
case 0:
message = "Child";
n=7;
execl("diffprog", 0);
break;
default:
message = "Parent";
n=5;
break;

}
for(;n>0; n--) {
puts(message);
sleep(1);
}
exit(0);

}


4)#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int lcm(int a, int b)
{
int temp=1, flag=0;
while(flag==0)
{
if(temp%a==0 && temp%b==0)
{
flag=1;
return temp;
}
else temp=temp+1;
}
}

int pow(int c, int d)
{
int i,result=1;
for(i=0;i<d;++i)
{
result=result*c;
}
return result;
}

int main()
{
int n1,n2;
int i;
int o1,o2;
pid_t pid;
static int fl=0;
printf("Enter the numbers :\n");
scanf("%d %d", &n1,&n2);
printf("Entered numbers are :%d\t%d\n",n1,n2);
printf("Fork started\n");
for (i=0;i<3;++i)
{
pid=fork();
switch(pid)
{
case -1:
perror("fork failed");
exit(1);
case 0: if(i==1)
{
o1=pow(n1,n2);
printf("\n%d power %d is: %d",n1,n2,o1);
fl=1;
}

if(i==2)
{
o2=lcm(n1,n2);
printf("\nLCM of %d %d is: %d",n1,n2,o2);

}
break;
default:exit(0);
break;
}
}
exit(0);
return 0;
}




LAB5B

2)#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include<signal.h>
int main(int argc, char* argv[])
{

pid_t pid;
pid = fork();
switch(pid)
{

case -1:

perror("Fork failed\n");

exit (1);

case 0:
printf("Child Process\n");
printf("Killing parent\n");
kill(getppid(), SIGKILL);
exit(0);

break;
default:
printf("\nChild Process created with id = %d\n",pid);
printf("Waiting for child to finish\n");
sleep(5);
printf("Child process finished\n");
break;

}
exit(0);
return 0;

}


3)#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include<signal.h>
int main(int argc, char* argv[])
{

pid_t pid;
printf("Starting fork\n");
pid = fork();
switch(pid)
{

case -1:
perror("\nFork failed\n");
exit (1);

case 0:
printf("\nChild Process\n");
sleep(5);
printf("\nChild completing execution\n");
exit(0);
break;

default:
printf("\nParent Process\n");
printf("\nChild Process created with id = %d\n",pid);
printf("\nParent will now wait for the child to complete\n");
printf("\nWaiting for child process to complete\n");
wait();
printf("\nChild process executed\n");
break;

}
exit(0);
return 0;

}

LAB6
1)#include <stdio.h>
#include <signal.h>
#include <sys/wait.h>
void Terminate(int sig_num)
{
signal(SIGINT, Terminate);
printf("\n Cannot be terminated by Ctrl+c\n");
fflush(stdout);
}


int main ()
{

signal(SIGINT, Terminate);
while(1); /* infinite loop */
return 0;
}



2)#include <stdio.h>
#include <signal.h>
#include<stdlib.h>
void CatchAlarm(int sig_num)
{
printf("\nTime Out\n");
exit(0);
}
int main ()
{
char username[10];
int i;
signal(SIGALRM, CatchAlarm);
printf("Enter your username: ");
fflush(stdout);
alarm(20);
fgets(username, 10, stdin);
alarm(0);
for(i=0;i<10;i++)
{
printf("\n The Username is : %c", username[i]);
}
return 0;
}

LAB7
1) #include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>

int main()
{
if(fork() == 0)
{
printf("\nChild id : %d\n",getpid());
printf("\nKilling process %d\n",getppid());
kill(getppid(), SIGINT);
}
else
{
printf("\nParent id : %d\n",getpid());
wait(0);
}

return(0);
}

2)#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>

time_t start_time;
time_t mytime;

void sigFunc(int sig)
{

printf("\nTIME OUT!\n");
alarm(5);
}

int main()
{
start_time=time(NULL);
signal(SIGALRM, sigFunc);
for(;;)
{
mytime = time(NULL);
printf(ctime(&mytime));
sleep(5);
}
alarm(0);
return 0;
}




LAB8B
1)#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 10

void *PrintHello(void *threadid)
{
int tid = (int)threadid;
int i;

for(i = 0; i < 5; i++){
printf("Hello, World (thread %d)\n", tid);
}
pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
int t;
for(t=0; t<NUM_THREADS; t++)
{
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}



pthread_exit(NULL);
}



2)#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void *SQRT()
{
int i;
double aq[100]={0.0},val=0.0;
for(i = 0; i <= 99; i++){
aq[i]=sqrt(i);
printf("\nSqrt of %d = %0.3lf",i,aq[i]);
}
return ((void *)aq);
}
int main (int argc, char *argv[])
{
pthread_t thread;
int i;
int rc =1 ;

double ret[100]={0.0};
rc = pthread_create(&thread, NULL, SQRT, NULL);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
pthread_join(thread, (void **)ret);
for(i=0;i<100;i++)
printf("\nSqrt of %d= %lf",i,ret[i]);
printf("\n");
pthread_exit(NULL);
}



LAB8A


1) #include <stdio.h>
#include <pthread.h>
#include<stdlib.h>

void* PrintHello(void* data)
{
int my_data = (int)data;

pthread_detach(pthread_self());
printf("Hello from new thread - got %d\n", my_data);
pthread_exit(NULL);
}


int main(int argc, char* argv[])
{
int rc;
pthread_t thread_id;
int t = 11;


rc = pthread_create(&thread_id, NULL, PrintHello, (void*)t);
if(rc)
{
printf("\n ERROR: return code from pthread_create is %d \n", rc);
exit(1);
}
printf("\n Created new thread (%d) ... \n", thread_id);

pthread_exit(NULL);
}

2)#include <stdio.h>
#include <pthread.h>

void* PrintHello(void* data)
{
int my_data = (int)data;
pthread_t tid=pthread_self();
pthread_detach(pthread_self());
printf("Hello from new thread (%d) - got %d\n",tid,my_data);
pthread_exit(NULL);
}


int main(int argc, char* argv[])
{
int rc;
pthread_t thread1_id,thread2_id;
// int t = 11;
thread1_id=pthread_self();

rc = pthread_create(&thread2_id, NULL, PrintHello, (void*)thread1_id);
if(rc)
{
printf("\n ERROR: return code from pthread_create is %d \n", rc);
exit(1);
}
printf("\nI am thread (%d) .Created new thread (%d) ... \n",thread1_id,thread2_id);

pthread_exit(NULL);
}


3)#include <stdio.h>
#include <pthread.h>

void* PrintHello(void* data)
{
int my_data = (int)data;
pthread_detach(pthread_self());
printf("Hello from new thread - got %d\n", my_data);

}


int main(int argc, char* argv[])
{
int rc;
pthread_t thread_id;
int t = 11;

rc = pthread_create(&thread_id, NULL, PrintHello, (void*)t);
if(rc)
{
printf("\n ERROR: return code from pthread_create is %d \n", rc);
exit(1);
}
sleep(1);
printf("\n Created new thread (%d) ... \n", thread_id);
pthread_exit(NULL);
}

4)#include <stdio.h>
#include <pthread.h>

void* PrintHello(void* data)
{
pthread_t tid = (pthread_t)data;

pthread_join(tid, NULL);
printf("Hello from new thread %d - got %d\n", pthread_self(), data);
pthread_exit(NULL);
}


int main(int argc, char* argv[])
{
int rc;
pthread_t thread_id;
int tid;

tid = pthread_self();

rc = pthread_create(&thread_id, NULL, PrintHello, (void*)tid);
if(rc)
{
printf("\n ERROR: return code from pthread_create is %d \n", rc);
exit(1);
}
sleep(1);
printf("\n Created new thread (%d) ... \n", thread_id);
pthread_exit(NULL);
}


5)#include <pthread.h>
#include <stdio.h>
#include<stdlib.h>
#define MAX_THREADS 50

void * PrintHello(void * data)
{
int a = (int)data;
printf("Hello from thread %d - I was created in iteration %d !\n", pthread_self(), a);
pthread_exit(NULL);
}

int main(int argc, char * argv[])
{
int rc;
pthread_t thread_id[MAX_THREADS];
int i, n;

n = atoi(argv[1]);
if(n > MAX_THREADS) n = MAX_THREADS;

for(i = 0; i < n; i++)
{
rc = pthread_create(&thread_id[i], NULL, PrintHello, (void*)i);
if(rc)
{
printf("\n ERROR: return code from pthread_create is %d \n", rc);
exit(1);
}
printf("\n I am thread %d. Created new thread (%d) in iteration %d ...\n",
pthread_self(), thread_id[i], i);
if(i % 4 == 0) sleep(1);
}

pthread_exit(NULL);
}

LAB8b
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 10

void *PrintHello(void *threadid)
{
int tid = (int)threadid;
int i;

for(i = 0; i < 5; i++){
printf("Hello, World (thread %d)\n", tid);
}
pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
int t;
for(t=0; t<NUM_THREADS; t++)
{
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}


/* Last thing that main() should do */
pthread_exit(NULL);
}

2)#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void *SQRT()
{
int i;
double aq[100]={0.0},val=0.0;
for(i = 0; i <= 99; i++){
aq[i]=sqrt(i);
printf("\nSqrt of %d = %0.3lf",i,aq[i]);
}
return ((void *)aq);
}
int main (int argc, char *argv[])
{
pthread_t thread;
int i;
int rc =1 ;

double ret[100]={0.0};
rc = pthread_create(&thread, NULL, SQRT, NULL);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
pthread_join(thread, (void **)ret);
for(i=0;i<100;i++)
printf("\nSqrt of %d= %lf",i,ret[i]);
printf("\n");
pthread_exit(NULL);
}

4)#include <pthread.h>
#include <unistd.h>
pthread_mutex_t lock ;
int shared_data ;
// Often shared data is more complex than just an int .
void *threadfunction ( void *arg )
{
int i ;
for(i=0;i<1024*1024;+i){
// Access the shared data here .

pthread_mutex_lock(&lock ) ;
shared_data++;
pthread_mutex_unlock( &lock ) ;

}
return NULL;
}
int main(void)
{
pthread_t thread_ID ;
void *thread_result ;
int i;
// Initialize the mutex before trying to use it .
pthread_mutex_init( &lock , NULL ) ;
pthread_create(&thread_ID , NULL, threadfunction , NULL ) ;
// Try to use the shared data .
for(i=0;i<10;++i ) {
sleep (1);
printf("\nLock acquired");
pthread_mutex_lock(&lock) ;
printf( "\nShared integer's value = %d\n" , shared_data) ;
pthread_mutex_unlock(&lock ) ;
printf("\nLock released");
}
printf( "\n" ) ;
pthread_join(thread_ID , &thread_result) ;
// Clean up the mutex when we are finished with it .
pthread_mutex_destroy(&lock ) ;
return 0 ;
}




LAB9
2)
#include <unistd.h>
#include<stdlib.h>
#include <stdio.h>
#include <string.h>
int main() {
int data_processed;
int file_pipes[2];
const char some_data[] = "123";
char buffer[BUFSIZ + 1];
pid_t fork_result;
memset(buffer, '\0', sizeof(buffer));
if (pipe(file_pipes) == 0) {
fork_result = fork();
if (fork_result == -1) {
fprintf(stderr, "Fork failure");
exit(EXIT_FAILURE); }
if (fork_result == 0) {
/* child */
data_processed = read(file_pipes[0], buffer, BUFSIZ);
printf("Read %d bytes: %s\n", data_processed, buffer);
exit(EXIT_SUCCESS); }
else {
/* parent */
data_processed = write(file_pipes[1], some_data,
strlen(some_data));
printf("Wrote %d bytes\n", data_processed); }
}
exit(EXIT_SUCCESS);
}

3)
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/wait.h>
#include<fcntl.h>

#define MAXLINE 80

void client(int , int );
void server(int, int);

int main(int argc, char **argv)
{
int pipe1[2], pipe2[2];
pid_t childpid;
/*create two pipes*/
pipe(pipe1);
pipe(pipe2);

if ( (childpid = fork()) == 0)
{ /*child*/
close(pipe1[1]);
close(pipe2[0]);

server(pipe1[0], pipe2[1]);
exit(0);
}

/*parent*/
close(pipe1[0]);
close(pipe2[1]);

client(pipe2[0], pipe1[1]);

waitpid(childpid, NULL, 0); /*wait for child to terminate*/
exit(0);
}

void client(int readfd, int writefd)
{
size_t len;
ssize_t n;
char buff[MAXLINE];

/*read the pathname*/
printf("enter the file name : ");
fgets(buff, MAXLINE, stdin);
len = strlen(buff);
if ( buff[len-1] == '\n' )
len--;
/*write the pathname to IPC channel*/
write(writefd, buff, len);

/*read from IPC channel, write to standard output*/
while ( (n = read(readfd, buff,MAXLINE)) > 0)
{
write(STDOUT_FILENO, buff, n);
}

}

void server(int readfd, int writefd)
{
int fd;
ssize_t n;
char buff[MAXLINE];

/*read pathname from IPC channel*/
if ( (n = read(readfd, buff, MAXLINE)) == 0)
{
printf("end of file while reading pathname from IPC\n");
return;
}

buff[n] = '\0';
if ( (fd = open(buff,O_RDONLY )) < 0)
{
strcat(buff, " : file can not open.\n");
n = strlen(buff);
write(writefd, buff, n);
}
else
{ /* open succeeded: copy file to IPC channel*/
while( (n = read(fd, buff, MAXLINE)) > 0 )
{
write(writefd, buff, n);
}
close(fd);
}
}


LAB 10
1)

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
int serid,sessid;
char c;
struct sockaddr_in server_address,client_address;
int server_addlen,client_addlen;
server_address.sin_family=AF_INET;
server_address.sin_addr.s_addr=inet_addr("127.0.0.1");
server_address.sin_port=5080;
server_addlen=sizeof(server_address);
client_addlen=sizeof(client_addlen);

serid=socket(AF_INET,SOCK_STREAM,0);
bind(serid,(struct sockaddr*)&server_address,server_addlen);
listen(serid,10);

while(1)
{
printf("Server is ready to accept ......\n");
sessid=accept(serid,(struct sockaddr *)&client_address,&client_addlen);
read(sessid,&c,1);
write(sessid,&c,1);
close(sessid);
}
return(0);
}

2)#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<netdb.h>
#include<stdio.h>
#include<time.h>
#define maxline 4096
#define LISTENQ 5
//#include <sum.h>

void str_echo(int);
struct args {
long arg1;
long arg2;
};




struct result {

long sum;

};

int main(int argc,char *argv)
{
int listenfd,connfd,bd;
pid_t childpid;
struct sockaddr_in servaddr;
char buff[maxline];
time_t ticks;

if(( listenfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("error");
}

bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(4500);

if((bd=bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr)))<0)
{
printf("\nbind error");
exit(0);
}

listen(listenfd,LISTENQ);

for(;;){

connfd=accept(listenfd,(struct sockaddr*)NULL,NULL);
if((childpid=fork())==0) {
close(listenfd);
str_echo(connfd);
exit(0);
}
close(connfd);

}
}

void str_echo(int sockfd)
{
ssize_t n;
struct args args;
struct result result;
char line[maxline];
for(;;) {
n= read(sockfd,&args,sizeof(args));
if(n==0)
return;
printf("the server are %ld%ld\n",args.arg1,args.arg2);









result.sum=args.arg1 + args.arg2;
// printf("the value returned by server is %ld\n ",result.sum);
// break;
//result.sum = args.arg1 - args.arg2;
//break;
write(sockfd,&result,sizeof(result));

}
}

3)#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/in.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>

float stof(const char* s);
float calculator(char *calculation);

extern int errno;

int main(int argc,char **argv)
{
int clientaddrlen, listenfd, connectfd, bytes_rcvd, listen_queue_size=1;
short int port_no;
char readBuff[1000];
struct sockaddr_in servaddr, clientaddr;
pid_t childpid;
int status;
char sendBuff[1025];

if(argc!=3){
printf("Usage Format: ./server -p <PortNumber>\n");
printf("Sample Run: ./server -p 2000\n");
exit(1);
}
port_no = atoi(argv[argc-1]);
printf("Server running at port #%d\n", port_no);

// Create server socket.
if ( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
fprintf(stderr, "Cannot create server socket! errno %i: %s\n",errno,strerror(errno));
exit(1);
}
printf("Server socket created\n");
// Bind (attach) this process to the server socket.
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(port_no);
errno = bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
if(errno < 0){
printf("Server bind failure errno %i: %s\n",errno,strerror(errno));
exit(1);
}
printf("Server socket is bound to port #%d\n", port_no);
// Turn 'listenfd' to a listening socket. Listen queue size is 1.
errno=listen(listenfd,listen_queue_size);
if(errno < 0){
printf("Server listen failure errno %i: %s\n",errno,strerror(errno));
exit(1);
}
printf("Server listening with a queue of size %d. \n", listen_queue_size);
// Wait for connection(s) from client(s).
while (1){
clientaddrlen = sizeof(clientaddr);
connectfd = accept(listenfd, (struct sockaddr *) &clientaddr, &clientaddrlen);
if(connectfd<0){
printf("Server accept failure errno %d: %s\n",errno,strerror(errno));
exit(1);
}
printf("A connection received from a client. Creating a child to serve the client.\n");
if((childpid = fork()) == 0) { /* child process */
close(listenfd); /* close listening socket */
printf("Child process serving the client.\n");
if (recv(connectfd, readBuff, sizeof(readBuff), 0 ) > 0){
printf("Received message: %s\n", readBuff);
float answer = calculator(readBuff);
char msg[60];
char chAns[30]="";
sprintf(chAns,"%f",answer);
strcat(msg,chAns);
write(connectfd,msg, strlen(readBuff));
}
//close(connectfd); /* parent closes connected socket */
exit(1);
}
else if (childpid <0){ /* failed to fork */
printf("Failed to fork\n");
printf("Fork error errno %d: %s\n",errno,strerror(errno));
}
else if(childpid != 0){ /* parent process */
close(connectfd); /* parent closes connected socket */
childpid = wait(&status);
}
}

S-ar putea să vă placă și