Showing posts with label UNIX PROGRAMS. Show all posts
Showing posts with label UNIX PROGRAMS. Show all posts

Tuesday, November 22

5 PROCESSES TO CHECK ARMSTRONG AND REVERSE OF NUMBER~ Unix Program

Hi reader,   Here our task is : Create 5 processes. Find whether a number is Armstong and Reverse the same using these processes. A :Accept number. C:Find whether it is armstrong. D:Print Result. B:Calculate Reverse of the Number. E:Display Reverse. Here is the complete Program. #include<stdio.h> #include<unistd.h> main() {             //initially in A int num,f1,f2,f3,f4,p1[2],p2[2],p3[2],p4[2],arm,rev,flag=0; pipe(p1); pipe(p2); f1=fork();  //A created B if(f1>0)    //A is working .. { f2=fork();  //A created...

Monday, October 10

SEMAPHORES TO IMPLEMENT PRODUCER CONSUMER PROBLEM

#include<sys/sem.h> #include<fcntl.h> #include<unistd.h> #include<sys/types.h> #include<stdio.h> #include<sys/ipc.h> void up(int); void down(int); union value { int val; struct semid_ds *buf; unsigned short *array; struct seminto *_buf; } main() { int mutex,full,empty,pid,i,key,j,k; union value arg; key=ftok("",'a'); mutex=semget(key,1,0660|IPC_CREAT); arg.val=0; semctl(full,0,SETVAL,arg); key=ftok("",'c'); empty=semget(key,1,0660|IPC_CREAT); arg.val=4; semctl(empty,0,SETVAL,arg); pid=fork(); switch(pid) { case -1: printf("Error"); case 0: sleep(13); printf("\n"); printf("Consumer consuming items\n"); for(i=0;i<5;i++) { down(full); down(mutex); for(j=4-i;j>=1;j--) printf("*"); for(k=0;k<=i;k++) printf("_"); printf("\n"); fflush(stdout); up(mutex); up(empty); sleep(1); } printf("Buffer...

Saturday, October 8

udp communication

Hi Viewer, Here is a simple java program showing client-server interacion using UDP. In tgis example, client will send a message and the sever will reply back with same message after converting it to Uppercase..!! server.java import java.io.*; import java.net.*; class UDPServer { public static void main(String args[])throws Exception { DatagramSocket serverSocket=new DatagramSocket(1249); byte[] receiveData=new byte[1024]; byte[] sendData=new byte[1024]; while(true) { DatagramPacket receivePacket=new DatagramPacket(receiveData,receiveData.length); serverSocket.receive(receivePacket); String sentence=new String(receivePacket.getData()); InetAddress...

Wednesday, September 21

Check a Number in parent process and pass result to Child Process

#include<stdio.h> #include<unistd.h> main() { int fd[2],f,i,num,flag=1;pipe(fd); f=fork(); if(f>0) { printf("PARENT PROCESS.."); printf("\nEnter a number : "); scanf("%d",&num); if(num==1)flag=0;for(i=2;i<=(num/2);i++) if(num%i==0) { flag=0; break; } close(fd[0]); write(fd[1],&flag,sizeof(flag)); } else if (f==0) { printf("\n\nCHILD PROCESS.");close(fd[1]); read(fd[0],&flag,sizeof(flag)); if(flag==1)printf(" \n\tIt is a prime number..\n"); else printf("\n\t It is not a prime number..\n"); } ...

Saturday, August 13

Create a Pipe and print descriptive value

program #include<stdio.h> main() { int f[2]; pipe(f); printf("Pipe Descriptive values are  %d and %d ", f[0],f[1]); }...

Create a Child process and print process-id

(i) Using one fork() system call #include<stdio.h> main() {       int f,c,p;       f=fork();       if(f==0)       {               c=getpid();               printf("Child Process\n pid: %d",c);               c=getppid();               printf("\tppid :%d \n",c);       }       else if(f>0)       {           ...

Sunday, July 31

C PROGRAM TO DISPLAY TYPE OF A FILE

Hi Friends, This is my C program to check the type of a file in UNIX system. Before going to program, have a look on what actually a 'type' means in UNIX system. A file may be one of the  following types :Regular File  : It may be either a text file or a binary file. Directory File : These are the files that can store other files (like file folder we use) so that users can organise their files into some hierarchical manner.!!) Character Device File : It refers to a physical device that transmits data in a character-based manner. (like printer, modems, consoles), Block Device File : Similar to character device...

Saturday, July 30

To Read Student Details and Make Rank List

Here is a simple C program that can be used to input students details. The program can store the details into a file and from that file it can make a rank list which can be written to another file.!! On reading the aim it is obvious that actual file handling may not be in same manner. But this program surely introduce you how Actual file 'read ' and 'write' occurs. Here we go.. STUDENT.C /**PROGRAM NAME:STUDENT.C*AIM : TO READ STUDENT DETAILS AND MAKE PROGRESS REPORT IN A FILE*OUTPUT FILE: STUDENT.TXT , PROGRESS. TXT*DATE: 07/30/2011*/ #include<stdio.h> #include<fcntl.h> struct student {       ...

Display the Contents of a Directory

    Here is a simple program to open and display the contents of a directory.                                               DIRECTORY.C /*PROGRAM NAME:DIRECTORY.CAIM:TO DISPLAY THE CONTENTS OF A DIRCTORYDATE:07/30/2011*/#include<stdio.h>#include<dirent.h> main(){char dirname[10];DIR *ptr;struct dirent *dir;printf("Enter the directory Name..");scanf("%s",dirname);ptr=opendir(dirname); printf("\nContents...