├── C-Programming ├── Baisc C programs │ ├── Age.c │ ├── Area.c │ ├── Introduction-to-c.md │ ├── primeornot.c │ └── searching_string_in_list_of_string.c ├── Chatapp using TCP │ ├── client.c │ └── server.c ├── Group Chat App │ ├── client.c │ └── server.c ├── IPC - Inter Process Communication │ ├── FIFO │ │ ├── FIFO-Client.c │ │ └── FIFO-Server.c │ ├── Message-Queue │ │ ├── msg-rec.c │ │ └── msg-send.c │ ├── Shared-memory │ │ ├── read.c │ │ └── write.c │ ├── Twoway-pipes.c │ └── oneway-pipes.c ├── Linux Schedular and Memory Management │ ├── FCFS scheduler.c │ ├── memory management.c │ └── priority-scheduling.c └── Multithread and interthread │ └── thread.c ├── README.md └── Shell script ├── basic-operations.sh ├── equal or not.sh ├── factorial.sh ├── select_pro.sh └── sum ofemployees.sh /C-Programming/Baisc C programs/Age.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int age; 5 | printf("Enter your age:"); 6 | scanf("%d",&age); 7 | if (age<=18 && age>=12) { 8 | printf("You are Teenager"); 9 | } 10 | else if(age>18){ 11 | printf("You are adult"); 12 | } 13 | else{ 14 | printf("You are Young"); 15 | } 16 | return 0; 17 | } -------------------------------------------------------------------------------- /C-Programming/Baisc C programs/Area.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | float a,b,h; 4 | printf("Enter Base (a) :"); 5 | scanf("%f",&a); 6 | printf("Enter Base (b) :"); 7 | scanf("%f",&b); 8 | printf("Enter Height (h) :"); 9 | scanf("%f",&h); 10 | printf("The Area of Trapizium is (A) : %.2f",((a+b)/2)*h); 11 | return 0; 12 | 13 | } 14 | -------------------------------------------------------------------------------- /C-Programming/Baisc C programs/Introduction-to-c.md: -------------------------------------------------------------------------------- 1 | # INTRODUCTION TO C PROGRAMMING 2 | 3 | ## Program to print a string 4 | 5 | ```c 6 | #include 7 | int main(){ 8 | printf("Hello world!"); 9 | return 0; 10 | } 11 | ``` 12 | 13 | ## Program to Arthimatic Operations 14 | 15 | ```c 16 | #include 17 | int main(){ 18 | int Num1,Num2; 19 | Num1=100; 20 | Num2=50; 21 | printf("Addtion of %d and %d is %d\n", Num1,Num2,Num1+Num2); 22 | printf("Subtraction of %d and %d is %d\n", Num1,Num2,Num1-Num2); 23 | printf("Multiplication of %d and %d is %d\n", Num1,Num2,Num1*Num2); 24 | printf("Division of %d and %d is %d\n",Num1,Num2,Num1/Num2); 25 | printf("Modulus of %d and %d is %d",Num1,Num2,Num1%Num2); 26 | return 0; 27 | } 28 | ``` 29 | ## Program to get your name and print the same 30 | ```c 31 | #include 32 | int main(){ 33 | char name[20]; 34 | printf("Enter your name:"); 35 | scanf("%s",&name); 36 | printf("your name is %s\n",name); 37 | return 0; 38 | } 39 | ``` 40 | 41 | ## Program to Get Age as Input and check its major or not 42 | 43 | ```c 44 | #include 45 | int main(){ 46 | int No; 47 | printf("Enter Your Age:"); 48 | scanf("%d",&No); 49 | if (No>=18){ 50 | printf("You are a major!\n"); 51 | } 52 | else if(No<18){ 53 | printf("You are not a major!\n"); 54 | } 55 | return 0; 56 | } 57 | ``` 58 | 59 | ## Program Get user age check wheather he is a adult , young or Teenager 60 | 61 | ```c 62 | #include 63 | int main(){ 64 | int age; 65 | printf("Enter your age:"); 66 | scanf("%d",&age); 67 | if (age<=18 && age>=12){ 68 | printf("You are a Teenager!\n"); 69 | } 70 | else if(age>=18) { 71 | printf("You are a adult!\n"); 72 | } 73 | else{ 74 | printf("You are young\n"); 75 | } 76 | } 77 | ``` 78 | ## Program return Given Number is Even or Not 79 | 80 | ```c 81 | #include 82 | int main(){ 83 | int no; 84 | printf("Enter Number:"); 85 | scanf("%d",&no); 86 | if(no%2==0){ 87 | print("Enter Number is Even Number\n"); 88 | } 89 | else { 90 | printf("Entered Number is a Odd Number\n"); 91 | } 92 | } 93 | ``` 94 | 95 | ## Program to find Area of Trapizium 96 | 97 | ```c 98 | #include 99 | int main(){ 100 | int a,b,h; 101 | printf("Enter Base (a) :"); 102 | scanf("%d",&a); 103 | printf("Enter Base (b) :"); 104 | scanf("%d",&b); 105 | printf("Enter Height (h) :"); 106 | scanf("%d",&h); 107 | printf("The Area of Trapizium is (A) : %d",((a+b)/2)*h); 108 | return 0; 109 | } 110 | ``` 111 | 112 | ## Program to find a string is present in a list of String or Not 113 | 114 | ```c 115 | #include 116 | #include 117 | int main() 118 | { 119 | char str[20][50],str1[50]; 120 | int i,no,found=0; 121 | 122 | printf("Enter of No of Names in the list:"); 123 | scanf("%d",&no); 124 | 125 | for (i=0;i 2 | int main () 3 | { 4 | int a,i; 5 | int flag=0; 6 | printf("Enter a Number to Check Prime or Not:"); 7 | scanf("%d",&a); 8 | for (i=2;i 2 | #include 3 | int main(){ 4 | // Declartion and initilization 5 | char str[20][50],str1[50]; 6 | int n,i,found=0; 7 | 8 | //No of Items in array 9 | printf("Enter How many Names in List:"); 10 | scanf("%d",&n); 11 | 12 | //Get the items of array 13 | for (i=0;i // inet_addr() 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include // bzero() 7 | #include 8 | #include // read(), write(), close() 9 | #define MAX 80 10 | #define PORT 8080 11 | #define SA struct sockaddr 12 | void func(int sockfd) 13 | { 14 | char buff[MAX]; 15 | int n; 16 | for (;;) { 17 | bzero(buff, sizeof(buff)); 18 | printf("Enter the string : "); 19 | n = 0; 20 | while ((buff[n++] = getchar()) != '\n') 21 | ; 22 | write(sockfd, buff, sizeof(buff)); 23 | bzero(buff, sizeof(buff)); 24 | read(sockfd, buff, sizeof(buff)); 25 | printf("From Server : %s", buff); 26 | if ((strncmp(buff, "exit", 4)) == 0) { 27 | printf("Client Exit...\n"); 28 | break; 29 | } 30 | } 31 | } 32 | 33 | int main() 34 | { 35 | int sockfd, connfd; 36 | struct sockaddr_in servaddr, cli; 37 | 38 | // socket create and verification 39 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 40 | if (sockfd == -1) { 41 | printf("socket creation failed...\n"); 42 | exit(0); 43 | } 44 | else 45 | printf("Socket successfully created..\n"); 46 | bzero(&servaddr, sizeof(servaddr)); 47 | 48 | // assign IP, PORT 49 | servaddr.sin_family = AF_INET; 50 | servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 51 | servaddr.sin_port = htons(PORT); 52 | 53 | // connect the client socket to server socket 54 | if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) 55 | != 0) { 56 | printf("connection with the server failed...\n"); 57 | exit(0); 58 | } 59 | else 60 | printf("connected to the server..\n"); 61 | 62 | // function for chat 63 | func(sockfd); 64 | 65 | // close the socket 66 | close(sockfd); 67 | } 68 | -------------------------------------------------------------------------------- /C-Programming/Chatapp using TCP/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include // read(), write(), close() 9 | #define MAX 80 10 | #define PORT 8080 11 | #define SA struct sockaddr 12 | 13 | // Function designed for chat between client and server. 14 | void func(int connfd) 15 | { 16 | char buff[MAX]; 17 | int n; 18 | // infinite loop for chat 19 | for (;;) { 20 | bzero(buff, MAX); 21 | 22 | // read the message from client and copy it in buffer 23 | read(connfd, buff, sizeof(buff)); 24 | // print buffer which contains the client contents 25 | printf("From client: %s\t To client : ", buff); 26 | bzero(buff, MAX); 27 | n = 0; 28 | // copy server message in the buffer 29 | while ((buff[n++] = getchar()) != '\n') 30 | ; 31 | 32 | // and send that buffer to client 33 | write(connfd, buff, sizeof(buff)); 34 | 35 | // if msg contains "Exit" then server exit and chat ended. 36 | if (strncmp("exit", buff, 4) == 0) { 37 | printf("Server Exit...\n"); 38 | break; 39 | } 40 | } 41 | } 42 | 43 | // Driver function 44 | int main() 45 | { 46 | int sockfd, connfd, len; 47 | struct sockaddr_in servaddr, cli; 48 | 49 | // socket create and verification 50 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 51 | if (sockfd == -1) { 52 | printf("socket creation failed...\n"); 53 | exit(0); 54 | } 55 | else 56 | printf("Socket successfully created..\n"); 57 | bzero(&servaddr, sizeof(servaddr)); 58 | 59 | // assign IP, PORT 60 | servaddr.sin_family = AF_INET; 61 | servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 62 | servaddr.sin_port = htons(PORT); 63 | 64 | // Binding newly created socket to given IP and verification 65 | if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) { 66 | printf("socket bind failed...\n"); 67 | exit(0); 68 | } 69 | else 70 | printf("Socket successfully binded..\n"); 71 | 72 | // Now server is ready to listen and verification 73 | if ((listen(sockfd, 5)) != 0) { 74 | printf("Listen failed...\n"); 75 | exit(0); 76 | } 77 | else 78 | printf("Server listening..\n"); 79 | len = sizeof(cli); 80 | 81 | // Accept the data packet from client and verification 82 | connfd = accept(sockfd, (SA*)&cli, &len); 83 | if (connfd < 0) { 84 | printf("server accept failed...\n"); 85 | exit(0); 86 | } 87 | else 88 | printf("server accept the client...\n"); 89 | 90 | // Function for chatting between client and server 91 | func(connfd); 92 | 93 | // After chatting close the socket 94 | close(sockfd); 95 | } 96 | -------------------------------------------------------------------------------- /C-Programming/Group Chat App/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include //for string operations 4 | #include //NULL constant defined here 5 | #include 6 | #include //for sockets 7 | #include //Internet Protocol family sockaddr_in defined here 8 | #include // for the cosy POSIX threads 9 | #include //for ctrl+c signal 10 | #include 11 | #include 12 | #define MYPORT 2012 /* default port number */ 13 | #define MAXDATALEN 256 14 | int sockfd; 15 | int n,x; /*variables for socket*/ 16 | struct sockaddr_in serv_addr; /* structure to hold server's address */ 17 | char buffer[MAXDATALEN]; 18 | char buf[10]; 19 | 20 | /* page 2*/ 21 | void *quitproc(); 22 | void* chat_write(int); 23 | void* chat_read(int); 24 | void *zzz(); 25 | int main(int argc, char *argv[]){ 26 | pthread_t thr1,thr2; 27 | if( argc != 2 ) { 28 | printf("help:u need to put server ip\n"); 29 | exit(0); 30 | } 31 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 32 | if (sockfd == -1){ 33 | printf ("client socket error\n"); 34 | } 35 | else{ 36 | printf("socket\t\tcreated\n"); 37 | } 38 | bzero((char *) &serv_addr, sizeof(serv_addr)); 39 | serv_addr.sin_family = AF_INET; 40 | serv_addr.sin_port = htons(MYPORT); 41 | serv_addr.sin_addr.s_addr = inet_addr(argv[1]); 42 | bzero(buf,10); 43 | printf("\nENTER YOUR NAME::"); 44 | fgets(buf,10,stdin); 45 | __fpurge(stdin); 46 | buf[strlen(buf)-1]=':'; 47 | if(connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))==-1){ 48 | printf("client connect error\n"); 49 | exit(0);} 50 | else 51 | printf("%s connected to server\n",buf); 52 | printf("\rYOU JOINED AS- %s",buffer-1); 53 | send(sockfd,buf,strlen(buf),0); 54 | pthread_create(&thr2,NULL,(void *)chat_write,(void *)sockfd); 55 | pthread_create(&thr1,NULL,(void *)chat_read,(void *)sockfd); 56 | pthread_join(thr2,NULL); 57 | pthread_join(thr1,NULL); 58 | return 0; 59 | } 60 | 61 | 62 | // chat_read 63 | void* chat_read(int sockfd) { 64 | if (signal(SIGINT,(void *)quitproc)==0) 65 | if(signal(SIGTSTP, (void *)zzz)==0) 66 | while(1){ 67 | n=recv(sockfd,buffer,MAXDATALEN-1,0); 68 | if(n==0){ 69 | printf("\nDUE TO SOME UNEXPECTED REASONS SERVER HAS BEEN SHUTDOWN\n\n"); 70 | exit(0);} 71 | 72 | if(n>0){ 73 | printf("\n%s ",buffer); 74 | bzero(buffer,MAXDATALEN); 75 | } 76 | }//while ends 77 | } 78 | 79 | void* chat_write(int sockfd){ 80 | while(1){ 81 | printf("%s",buf); 82 | fgets(buffer,MAXDATALEN-1,stdin); 83 | if(strlen(buffer)-1>sizeof(buffer)){ 84 | printf("buffer size full \t enter within %lu characters \n",sizeof(buffer)); 85 | bzero(buffer,MAXDATALEN); 86 | __fpurge(stdin); 87 | } 88 | n=send(sockfd,buffer,strlen(buffer),0); 89 | if(strncmp(buffer,"quit",4)==0) 90 | exit(0); 91 | 92 | bzero(buffer,MAXDATALEN); 93 | } 94 | 95 | } 96 | 97 | void *quitproc(){ //handling ctrl+d 98 | printf("\rPLEASE TYPE 'quit' TO EXIT\n"); 99 | } 100 | 101 | void *zzz(){ //handling ctrl+z 102 | printf("\rPLEASE TYPE 'quit' TO EXIT\n"); 103 | } 104 | 105 | -------------------------------------------------------------------------------- /C-Programming/Group Chat App/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include //for sockets 4 | #include 5 | #include //for string operations 6 | #include //Internet Protocol family sockaddr_in defined here 7 | #include // for the cosy POSIX threads 8 | #include // for inet_ntoa() function 9 | #include //NULL constant defined here 10 | #include //for ctrl+c signal 11 | #define BACKLOG 100 // connections in the queue 12 | #define MAXDATALEN 256 //max size of messages to be sent 13 | #define PORT 2012 //default port number 14 | struct Node /*structure to handle all clients*/ 15 | { 16 | int port; 17 | char username[10]; 18 | struct Node *next; 19 | }; 20 | 21 | typedef struct Node *ptrtonode; 22 | typedef ptrtonode head; 23 | typedef ptrtonode addr; 24 | 25 | 26 | void sendtoall(char *,int new_fd); /*send chat msgs to all connected clients*/ 27 | void Quitall( ); /*send msg to all if server shuts down*/ 28 | head MakeEmpty( head h ); /*clearing list*/ 29 | void Delete( int port, head h ); /*delete client values on client exit*/ 30 | void Insert(int port,char*,head h,addr a);/*inserting new client */ 31 | void DeleteList( head h ); /*clearing list*/ 32 | void Display( const head h ); /*list all clients connected*/ 33 | void *Quitproc( ); /*signal handler*/ 34 | void *server(void * arg); /*server instance for every connected client*/ 35 | void zzz(); 36 | 37 | char username[10]; /*size of username*/ 38 | int sf2; 39 | head h; /*variable of type struct head*/ 40 | char buffer[MAXDATALEN]; 41 | /******main starts ***********/ 42 | int main(int argc, char *argv[]){ 43 | int sockfd,new_fd; /*variables for socket*/ 44 | int portnum; /*variable for port numb if provided*/ 45 | struct sockaddr_in server_addr; /*structure to hold server's address */ 46 | struct sockaddr_in client_addr; /*structure to hold client's address */ 47 | int cli_size,z; /*length of address */ 48 | pthread_t thr; /*variable to hold thread ID */ 49 | int yes=1; 50 | addr a; 51 | printf("\n\t*-*-*-SERVER STARTED*-*-*-*\n"); 52 | /*=optional or default port argument=*/ 53 | if( argc == 2 ) 54 | portnum = atoi(argv[1]); 55 | else 56 | portnum = PORT; //if port number not given as argument then using default port 57 | printf("PORT NO.:\t%d\n",portnum); 58 | h = MakeEmpty( NULL ); //frees the list 59 | /*=set info of server =*/ 60 | server_addr.sin_family=AF_INET; /* set family to Internet */ 61 | server_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* set IP address */ 62 | server_addr.sin_port=htons(portnum); 63 | printf("IP ADDRESS:\t%s\n",inet_ntoa(server_addr.sin_addr)); 64 | /*=creating socket=*/ 65 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 66 | if(sockfd == -1){ 67 | printf("server- socket() error"); // debugging 68 | exit(1);} 69 | else 70 | printf("socket\t\tcreated.\n"); 71 | 72 | if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,sizeof(int)) == -1) { 73 | printf("setsockopt error"); // debugging 74 | exit(1); 75 | } 76 | else{ 77 | printf("reusing\t\tport\n");} 78 | /*=binding socket=*/ 79 | if(bind(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))==-1){ 80 | printf("binding failed\n"); 81 | exit(1);} 82 | else{ 83 | printf("binding\t\tsuccess.\n\n");} 84 | printf("\t\tPRESS CTRL+z TO VIEW ONLINE CLIENTS\n\n"); 85 | /*=socket on listening mode=*/ 86 | listen(sockfd, BACKLOG); 87 | printf("waiting for clients......\n"); 88 | if (signal(SIGINT,(void *)Quitproc)==0) //signal handler 89 | if(signal(SIGTSTP, zzz)==0) //signal handler 90 | while(1){ 91 | cli_size=sizeof(struct sockaddr_in); 92 | //cli_size necessary as an argument for pthread_create 93 | new_fd = accept(sockfd, (struct sockaddr *)&client_addr,&cli_size); 94 | //accepting connection from client 95 | a=h ; 96 | /*=sign in with name=*/ 97 | bzero(username,10); 98 | if(recv(new_fd,username,sizeof(username),0)>0); 99 | username[strlen(username)-1]=':'; 100 | printf("\t%d->%s JOINED chatroom\n",new_fd,username); 101 | sprintf(buffer,"%s IS ONLINE\n",username); 102 | Insert( new_fd,username, h, a ); //inserting newly accepted client socked fd in list 103 | a = a->next; 104 | 105 | /*=notify all clients about newly joining clients=*/ 106 | a = h ; 107 | do{ 108 | a = a->next; 109 | sf2 = a->port; 110 | if(sf2!=new_fd) 111 | send(sf2,buffer ,sizeof(buffer),0); 112 | } 113 | while( a->next != NULL ); 114 | printf("server got connection from %s & %d\n\n",inet_ntoa(client_addr.sin_addr),new_fd); 115 | // debugging 116 | struct Node args; //struct to pass multiple arguments to server function 117 | args.port=new_fd; 118 | strcpy(args.username,username); 119 | pthread_create(&thr,NULL,server,(void*)&args); //creating thread for every client connected 120 | pthread_detach(thr); 121 | } /*while end*/ 122 | DeleteList(h); //deleting all clients when server closes 123 | close(sockfd); 124 | } 125 | 126 | /* ==========Server function for every connected Client =========*/ 127 | void *server(void * arguments){ 128 | struct Node *args=arguments; 129 | char buffer[MAXDATALEN],ubuf[50],uname[10]; /* buffer for string the server sends*/ 130 | char *strp; 131 | char *msg = (char *) malloc(MAXDATALEN); 132 | int ts_fd,x,y; 133 | int sfd,msglen; 134 | ts_fd = args->port; /*socket variable passed as arg*/ 135 | strcpy(uname,args->username); 136 | addr a; 137 | /*=sending list of clients online=*/ 138 | a =h ; 139 | do{ 140 | a = a->next; 141 | sprintf( ubuf," %s is online\n",a->username ); 142 | send(ts_fd,ubuf,strlen(ubuf),0); 143 | } 144 | while( a->next != NULL ); 145 | /*=start chatting=*/ 146 | while(1){ 147 | bzero(buffer,256); 148 | y=recv(ts_fd,buffer,MAXDATALEN,0); 149 | if (y==0) 150 | goto jmp; 151 | /*=if a client quits=*/ 152 | if ( strncmp( buffer, "quit", 4) == 0 ){ 153 | jmp: printf("%d ->%s left chat deleting from list\n",ts_fd,uname); 154 | sprintf(buffer,"%s has left the chat\n",uname); 155 | addr a = h ; 156 | do{ 157 | a = a->next; 158 | sfd = a->port; 159 | if(sfd == ts_fd) 160 | Delete( sfd, h ); 161 | if(sfd != ts_fd) 162 | send(sfd,buffer,MAXDATALEN,0); 163 | } 164 | while ( a->next != NULL ); 165 | Display( h ); 166 | close(ts_fd); 167 | free(msg); 168 | break; 169 | } 170 | /*=sending message to all clients =*/ 171 | printf("%s %s\n",uname,buffer); 172 | strcpy(msg,uname); 173 | x=strlen(msg); 174 | strp = msg; 175 | strp+= x; 176 | strcat(strp,buffer); 177 | msglen=strlen(msg); 178 | 179 | addr a = h ; 180 | do{ 181 | a = a->next; 182 | sfd = a->port; 183 | if(sfd != ts_fd) 184 | send(sfd,msg,msglen,0); 185 | } while( a->next != NULL ); 186 | Display( h ); 187 | bzero(msg,MAXDATALEN); 188 | }//end while 189 | return 0; 190 | } //end server 191 | 192 | /*=====empties and deletes the list======*/ 193 | head MakeEmpty( head h ){ 194 | if( h != NULL ) 195 | DeleteList( h ); 196 | h = malloc( sizeof( struct Node ) ); 197 | if( h == NULL ) 198 | printf( "Out of memory!" ); 199 | h->next = NULL; 200 | return h; 201 | } 202 | 203 | /*======delete list=======*/ 204 | void DeleteList( head h ){ 205 | addr a, Tmp; 206 | a = h->next; 207 | h->next = NULL; 208 | while( a != NULL ){ 209 | Tmp = a->next; 210 | free( a ); 211 | a = Tmp; 212 | } 213 | } 214 | 215 | /*===============inserting new clients to list==========*/ 216 | void Insert( int port,char *username, head h, addr a ) 217 | { 218 | addr TmpCell; 219 | TmpCell = malloc( sizeof( struct Node ) ); 220 | if( TmpCell == NULL ) 221 | printf( "Out of space!!!" ); 222 | TmpCell->port = port; 223 | strcpy(TmpCell->username,username); 224 | TmpCell->next = a->next; 225 | a->next = TmpCell; 226 | } 227 | 228 | /*========displaying all clients in list==================*/ 229 | void Display( const head h ){ 230 | addr a =h ; 231 | if( h->next == NULL ) 232 | printf( "NO ONLINE CLIENTS\n" ); 233 | else{ 234 | do{ 235 | a = a->next; 236 | printf( "%d->%s \t", a->port,a->username ); 237 | } while( a->next != NULL ); 238 | printf( "\n" ); 239 | } 240 | } 241 | 242 | 243 | /*===========client deleted from list if client quits================*/ 244 | void Delete( int port, head h ){ 245 | addr a, TmpCell; 246 | a = h; 247 | while( a->next != NULL && a->next->port != port ) 248 | a = a->next; 249 | if( a->next != NULL ){ 250 | TmpCell = a->next; 251 | a->next = TmpCell->next; 252 | free( TmpCell ); 253 | } 254 | } 255 | 256 | /*======handling signals==========*/ 257 | void *Quitproc(){ 258 | printf("\n\nSERVER SHUTDOWN\n"); 259 | Quitall( ); 260 | exit(0); 261 | } 262 | 263 | /*===============notifying server shutdown===========*/ 264 | void Quitall(){ 265 | int sfd; 266 | addr a = h ; 267 | int i=0; 268 | if( h->next == NULL ) { 269 | printf( "......BYE.....\nno clients \n\n" ); 270 | exit(0); 271 | } 272 | else { 273 | do{ 274 | i++; 275 | a = a->next; 276 | sfd = a->port; 277 | send(sfd,"server down",13,0); 278 | } while( a->next != NULL ); 279 | printf("%d clients closed\n\n",i); 280 | } 281 | } 282 | 283 | void zzz(){ 284 | printf("\rDISPLAYING ONLINE CLIENTS\n\n"); 285 | Display(h); 286 | } 287 | -------------------------------------------------------------------------------- /C-Programming/IPC - Inter Process Communication/FIFO/FIFO-Client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | int main(){ 8 | int fd1; 9 | // FIFO file path 10 | char * myfifo = "/tmp/myfifo"; 11 | // Creating the named file(FIFO) 12 | // mkfifo(,) 13 | mkfifo(myfifo, 0666); 14 | char str1[80], str2[80]; 15 | while (1){ 16 | // First open in read only and read 17 | fd1 = open(myfifo,O_RDONLY); 18 | read(fd1, str1, 80); 19 | // Print the read string and close 20 | printf("User1: %s\n", str1); 21 | close(fd1); 22 | // Now open in write mode and write 23 | // string taken from user. 24 | fd1 = open(myfifo,O_WRONLY); 25 | fgets(str2, 80, stdin); 26 | write(fd1, str2, strlen(str2)+1); 27 | close(fd1); 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /C-Programming/IPC - Inter Process Communication/FIFO/FIFO-Server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | int main(){ 8 | int fd; 9 | char * myfifo = "/tmp/myfifo"; // FIFO FIle Path 10 | // Creating the named file (FIFO) 11 | // mkfifo (,) 12 | mkfifo(myfifo,0666); 13 | char arr1[80],arr2[80]; 14 | while(1){ 15 | // Open FIFO for write only 16 | fd=open(myfifo,O_WRONLY); 17 | // Take an Input arr2ing from user 18 | // 80 is maximum length 19 | fgets(arr2,80,stdin); 20 | // Write the input arr2ing on FIFO and close it 21 | write(fd,arr2,strlen(arr2)+1); 22 | close(fd); 23 | // Open FIFO for Read Only 24 | fd = open(myfifo,O_RDONLY); 25 | // Read from FIFO 26 | read(fd,arr1,sizeof(arr1)); 27 | // Print the read message 28 | printf("User2:%s\n",arr1); 29 | close(fd); 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /C-Programming/IPC - Inter Process Communication/Message-Queue/msg-rec.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define PERMS 0644 9 | struct my_msgbuf { 10 | long mtype; 11 | char mtext[200]; 12 | }; 13 | 14 | int main(void) { 15 | struct my_msgbuf buf; 16 | int msqid; 17 | int toend; 18 | key_t key; 19 | 20 | if ((key = ftok("msgq.txt", 'B')) == -1) { 21 | perror("ftok"); 22 | exit(1); 23 | } 24 | 25 | if ((msqid = msgget(key, PERMS)) == -1) { /* connect to the queue */ 26 | perror("msgget"); 27 | exit(1); 28 | } 29 | printf("message queue: ready to receive messages.\n"); 30 | 31 | for(;;) { /* normally receiving never ends but just to make conclusion 32 | /* this program ends wuth string of end */ 33 | if (msgrcv(msqid, &buf, sizeof(buf.mtext), 0, 0) == -1) { 34 | perror("msgrcv"); 35 | exit(1); 36 | } 37 | printf("recvd: \"%s\"\n", buf.mtext); 38 | toend = strcmp(buf.mtext,"end"); 39 | if (toend == 0) 40 | break; 41 | } 42 | printf("message queue: done receiving messages.\n"); 43 | system("rm msgq.txt"); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /C-Programming/IPC - Inter Process Communication/Message-Queue/msg-send.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define PERMS 0644 11 | struct my_msgbuf { 12 | long mtype; 13 | char mtext[200]; 14 | }; 15 | 16 | int main(void) { 17 | struct my_msgbuf buf; 18 | int msqid; 19 | int len; 20 | key_t key; 21 | system("touch msgq.txt"); 22 | 23 | if ((key = ftok("msgq.txt", 'B')) == -1) { 24 | perror("ftok"); 25 | exit(1); 26 | } 27 | 28 | if ((msqid = msgget(key, PERMS | IPC_CREAT)) == -1) { 29 | perror("msgget"); 30 | exit(1); 31 | } 32 | printf("message queue: ready to send messages.\n"); 33 | printf("Enter lines of text, ^D to quit:\n"); 34 | buf.mtype = 1; /* we don't really care in this case */ 35 | 36 | while(fgets(buf.mtext, sizeof buf.mtext, stdin) != NULL) { 37 | len = strlen(buf.mtext); 38 | /* remove newline at end, if it exists */ 39 | if (buf.mtext[len-1] == '\n') buf.mtext[len-1] = '\0'; 40 | if (msgsnd(msqid, &buf, len+1, 0) == -1) /* +1 for '\0' */ 41 | perror("msgsnd"); 42 | } 43 | strcpy(buf.mtext, "end"); 44 | len = strlen(buf.mtext); 45 | if (msgsnd(msqid, &buf, len+1, 0) == -1) /* +1 for '\0' */ 46 | perror("msgsnd"); 47 | 48 | if (msgctl(msqid, IPC_RMID, NULL) == -1) { 49 | perror("msgctl"); 50 | exit(1); 51 | } 52 | printf("message queue: done sending messages.\n"); 53 | return 0; 54 | } -------------------------------------------------------------------------------- /C-Programming/IPC - Inter Process Communication/Shared-memory/read.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define BUF_SIZE 1024 10 | #define SHM_KEY 0x1234 11 | 12 | struct shmseg { 13 | int cnt; 14 | int complete; 15 | char buf[BUF_SIZE]; 16 | }; 17 | 18 | int main(int argc, char *argv[]) { 19 | int shmid; 20 | struct shmseg *shmp; 21 | shmid = shmget(SHM_KEY, sizeof(struct shmseg), 0644|IPC_CREAT); 22 | if (shmid == -1) { 23 | perror("Shared memory"); 24 | return 1; 25 | } 26 | 27 | // Attach to the segment to get a pointer to it. 28 | shmp = shmat(shmid, NULL, 0); 29 | if (shmp == (void *) -1) { 30 | perror("Shared memory attach"); 31 | return 1; 32 | } 33 | 34 | /* Transfer blocks of data from shared memory to stdout*/ 35 | while (shmp->complete != 1) { 36 | printf("segment contains : \n\"%s\"\n", shmp->buf); 37 | if (shmp->cnt == -1) { 38 | perror("read"); 39 | return 1; 40 | } 41 | printf("Reading Process: Shared Memory: Read %d bytes\n", shmp->cnt); 42 | sleep(3); 43 | } 44 | printf("Reading Process: Reading Done, Detaching Shared Memory\n"); 45 | if (shmdt(shmp) == -1) { 46 | perror("shmdt"); 47 | return 1; 48 | } 49 | printf("Reading Process: Complete\n"); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /C-Programming/IPC - Inter Process Communication/Shared-memory/write.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define BUF_SIZE 1024 12 | #define SHM_KEY 0x1234 13 | 14 | struct shmseg { 15 | int cnt; 16 | int complete; 17 | char buf[BUF_SIZE]; 18 | }; 19 | int fill_buffer(char * bufptr, int size); 20 | 21 | int main(int argc, char *argv[]) { 22 | int shmid, numtimes; 23 | struct shmseg *shmp; 24 | char *bufptr; 25 | int spaceavailable; 26 | shmid = shmget(SHM_KEY, sizeof(struct shmseg), 0644|IPC_CREAT); 27 | if (shmid == -1) { 28 | perror("Shared memory"); 29 | return 1; 30 | } 31 | 32 | // Attach to the segment to get a pointer to it. 33 | shmp = shmat(shmid, NULL, 0); 34 | if (shmp == (void *) -1) { 35 | perror("Shared memory attach"); 36 | return 1; 37 | } 38 | 39 | /* Transfer blocks of data from buffer to shared memory */ 40 | bufptr = shmp->buf; 41 | spaceavailable = BUF_SIZE; 42 | for (numtimes = 0; numtimes < 5; numtimes++) { 43 | shmp->cnt = fill_buffer(bufptr, spaceavailable); 44 | shmp->complete = 0; 45 | printf("Writing Process: Shared Memory Write: Wrote %d bytes\n", shmp->cnt); 46 | bufptr = shmp->buf; 47 | spaceavailable = BUF_SIZE; 48 | sleep(3); 49 | } 50 | printf("Writing Process: Wrote %d times\n", numtimes); 51 | shmp->complete = 1; 52 | 53 | if (shmdt(shmp) == -1) { 54 | perror("shmdt"); 55 | return 1; 56 | } 57 | 58 | if (shmctl(shmid, IPC_RMID, 0) == -1) { 59 | perror("shmctl"); 60 | return 1; 61 | } 62 | printf("Writing Process: Complete\n"); 63 | return 0; 64 | } 65 | 66 | int fill_buffer(char * bufptr, int size) { 67 | static char ch = 'A'; 68 | int filled_count; 69 | 70 | //printf("size is %d\n", size); 71 | memset(bufptr, ch, size - 1); 72 | bufptr[size-1] = '\0'; 73 | if (ch > 122) 74 | ch = 65; 75 | if ( (ch >= 65) && (ch <= 122) ) { 76 | if ( (ch >= 91) && (ch <= 96) ) { 77 | ch = 65; 78 | } 79 | } 80 | filled_count = strlen(bufptr); 81 | 82 | //printf("buffer count is: %d\n", filled_count); 83 | //printf("buffer filled is:%s\n", bufptr); 84 | ch++; 85 | return filled_count; 86 | } -------------------------------------------------------------------------------- /C-Programming/IPC - Inter Process Communication/Twoway-pipes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(){ 4 | int pipefds1[2],pipefds2[2]; 5 | int returnstatus1,returnstatus2; 6 | int pid; 7 | char pipe1writemessage[20]="Hi"; 8 | char pipe2writemessage[20]="Hello"; 9 | char readmessage[20]; 10 | returnstatus1 = pipe(pipefds1); 11 | if (returnstatus1==-1){ 12 | printf("Unable to create pipe 1 \n"); 13 | return 1; 14 | } 15 | returnstatus2=pipe(pipefds2); 16 | if (returnstatus2==-1){ 17 | printf("Unable to create pipe 2\n"); 18 | return 1; 19 | } 20 | pid=fork(); 21 | if(pid!=0){ 22 | close(pipefds1[0]); 23 | close(pipefds2[1]); 24 | printf("In Parent Writing to pipe 1 - Message is %s\n",pipe1writemessage); 25 | write(pipefds1[1],pipe1writemessage,sizeof(pipe1writemessage)); 26 | read(pipefds2[0],readmessage,sizeof(readmessage)); 27 | printf("In Parent Reading from pipe 2 -Message is %s\n",readmessage); 28 | } 29 | else{ 30 | close(pipefds1[1]); 31 | close(pipefds2[0]); 32 | read(pipefds1[0],readmessage,sizeof(readmessage)); 33 | printf("In Child:Reading from pipe 1 - Message is %s\n",readmessage); 34 | printf("In Child:Writing to pipe 2- Message is %s\n",pipe2writemessage); 35 | write(pipefds2[1],pipe2writemessage,sizeof(pipe2writemessage)); 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /C-Programming/IPC - Inter Process Communication/oneway-pipes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(){ 7 | int fd[2],n; 8 | char buffer[100]; 9 | pid_t p; 10 | pipe(fd); //creates a unidirectional pipe with two end fd[0] and fd[1] 11 | p=fork(); 12 | if(p>0){ 13 | printf("Parent Passing value to child\n"); 14 | write(fd[1],"Hello\n",6); //fd[1] is the write end of the pipe 15 | wait(NULL); 16 | } 17 | else{ 18 | printf("Child printing recieved value\n"); 19 | n=read(fd[0],buffer,100); //fd[0] is the read end of the pipe 20 | write(1,buffer,n); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /C-Programming/Linux Schedular and Memory Management/FCFS scheduler.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main(){ 5 | int i,n,*bt,*wt,*tat; 6 | float avgtat,avgwt; 7 | printf("\nEnter the number of processes:"); 8 | scanf("%d",&n); 9 | bt=(int*)malloc(n*sizeof(int)); 10 | wt=(int*)malloc(n*sizeof(int)); 11 | tat=(int*)malloc(n*sizeof(int)); 12 | printf("\nEnter the burst time for each process\n"); 13 | for(i=0;i 2 | #include 3 | int main(){ 4 | int n,i,*ptr,sum=0; 5 | printf("Enter number of elements:"); 6 | scanf("%d",&n); 7 | ptr=(int*)malloc(n*sizeof(int)); 8 | if(ptr==NULL){ 9 | printf("Sorry! unable to allocate memory"); 10 | exit(0); 11 | } 12 | for (i=0;i 6 | 7 | //Function to swap two variables 8 | void swap(int *a,int *b) 9 | { 10 | int temp=*a; 11 | *a=*b; 12 | *b=temp; 13 | } 14 | int main() 15 | { 16 | int n; 17 | printf("Enter Number of Processes: "); 18 | scanf("%d",&n); 19 | 20 | // b is array for burst time, p for priority and index for process id 21 | int b[n],p[n],index[n]; 22 | for(int i=0;i a) 36 | { 37 | a=p[j]; 38 | m=j; 39 | } 40 | } 41 | 42 | //Swapping processes 43 | swap(&p[i], &p[m]); 44 | swap(&b[i], &b[m]); 45 | swap(&index[i],&index[m]); 46 | } 47 | 48 | // T stores the starting time of process 49 | int t=0; 50 | 51 | //Printing scheduled process 52 | printf("Order of process Execution is\n"); 53 | for(int i=0;i 2 | #include 3 | #include 4 | 5 | #define NUM_THREADS 2 6 | 7 | /* create thread argument struct for thr_func() */ 8 | typedef struct _thread_data_t { 9 | int tid; 10 | double stuff; 11 | } thread_data_t; 12 | 13 | /* thread function */ 14 | void *thr_func(void *arg) { 15 | thread_data_t *data = (thread_data_t *)arg; 16 | 17 | printf("hello from thr_func, thread id: %d\n", data->tid); 18 | 19 | pthread_exit(NULL); 20 | } 21 | 22 | int main(int argc, char **argv) { 23 | pthread_t thr[NUM_THREADS]; 24 | int i, rc; 25 | /* create a thread_data_t argument array */ 26 | thread_data_t thr_data[NUM_THREADS]; 27 | 28 | /* create threads */ 29 | for (i = 0; i < NUM_THREADS; ++i) { 30 | thr_data[i].tid = i; 31 | if ((rc = pthread_create(&thr[i], NULL, thr_func, &thr_data[i]))) { 32 | fprintf(stderr, "error: pthread_create, rc: %d\n", rc); 33 | return EXIT_FAILURE; 34 | } 35 | } 36 | /* block until all threads complete */ 37 | for (i = 0; i < NUM_THREADS; ++i) { 38 | pthread_join(thr[i], NULL); 39 | } 40 | 41 | return EXIT_SUCCESS; 42 | } 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Basic Linux Commands 2 | 3 | - Take me to the [Video Tutorial](https://kodekloud.com/topic/basic-linux-commands-3/) 4 | 5 | In this section, we will take a look at basic linux commands 6 | - Specifically related to navigation and creating new files and directories. 7 | - We will do this by completing a simple task using a linux shell. 8 | 9 | #### Our goal is to create a directory structure, the top most directory which is **`/home/michael`** which is already created as it as a home directory but everything else underneath has to be created. 10 | 11 | ![mkdir_cd_working_with_shell_I](../../images/mkdir_cd_working_with_shell_I.PNG) 12 | 13 | To print the present working directory. Run **`pwd`** command 14 | ``` 15 | $ pwd 16 | ``` 17 | 18 | To see the contents of the directory. Run **`ls`** command 19 | ``` 20 | $ ls 21 | ```` 22 | 23 | To make (or) create a directory. Run **`mkdir`** command 24 | ``` 25 | $ mkdir Asia 26 | ``` 27 | 28 | To make (or) create multiple directories. Run **`mkdir`** command followed by **` .. `** 29 | ``` 30 | $ mkdir Europe Africa America 31 | ``` 32 | 33 | To change a directory from the current directory. Run **`cd `** 34 | ``` 35 | $ cd Asia 36 | ``` 37 | 38 | To recursively created directories. Run **`mkdir -p /`** 39 | ``` 40 | $ mkdir -p India/Mumbai 41 | ``` 42 | 43 | To go back to one directory up. Run **`cd ..`** 44 | ``` 45 | $ cd .. 46 | ``` 47 | 48 | To go back directly to a home directory of the current user from any location in the system. Run **`cd`** 49 | ``` 50 | $ cd 51 | ``` 52 | 53 | #### Lets now look at absolute path and relative path 54 | 55 | 56 | 57 | ![Absolute_and_relative_path_working_with_shell_I](../../images/Absolute_and_relative_path_working_with_shell_I.PNG) 58 | 59 | **Difference Between Absolute and Relative Path** 60 | 61 | - **Absolute Path** : An absolute path is defined as specifying the location of a file or directory from the root directory(/). 62 | - **Relative Path** : Relative path is defined as the path related to the present working directly(pwd). 63 | 64 | To change to a directory with absolute path. Run **`cd `** 65 | ``` 66 | $ cd /home/michael 67 | ``` 68 | 69 | To Change to a directory with relative path. Run **`cd `** 70 | ``` 71 | $ cd Asia 72 | ``` 73 | 74 | #### Lets now take a look at alternatives to the **`cd`** command 75 | 76 | ![pushd_popd](../../images/pushd_popd.PNG) 77 | 78 | Alternative to the **`cd`** is the **`pushd\popd`** command. To change directory using pushd, run **`pushd `** 79 | ``` 80 | $ pushd /etc 81 | ``` 82 | 83 | You can change to subdirecties under /etc as many times as you wish 84 | ``` 85 | $ pushd /var 86 | $ pushd /tmp 87 | $ pwd 88 | /etc/var/tmp 89 | ``` 90 | 91 | To return back to origin directory(say your home directory), use the **`popd`** command 92 | ``` 93 | $ popd 94 | ``` 95 | 96 | #### Now lets move on to look some more basic commands in linux. To learn these commands we will make use of the same directory structure as before, however now there are some new files and directories added as shown in the diagram. The goal of this task is to make sure the directory structure looks like the below diagram . 97 | 98 | ![before_after_commands](../../images/before_after_commands.PNG) 99 | 100 | To move file or directory. Run **`mv `** command 101 | ``` 102 | $ mv /home/michael/Europe/Morocco /home/michael/Africa/ (Absolute path) 103 | $ mv Europe/Morocco Africa/ (Relative Path) 104 | ``` 105 | 106 | To rename a directory. Run **`mv `** command 107 | ``` 108 | $ mv Asia/India/Munbai Asia/India/Mumbai 109 | ``` 110 | 111 | To copy a file to a directory. Run **`cp `** command 112 | ``` 113 | $ cp Asia/India/Mumbai/City.txt Africa/Egypt/Cairo 114 | ``` 115 | 116 | To delete a file from a directory. Run **`rm /path/`** command 117 | ``` 118 | $ rm Europe/UK/London/Tottenham.txt 119 | ``` 120 | 121 | To copy a directory recursively. Run **`cp -r `** command 122 | ``` 123 | $ cp -r Europe/UK Europe/UnitedKingdom 124 | ``` 125 | 126 | To print the content of a file. Run **`cat /path/to/`** command 127 | ``` 128 | $ cat Asia/India/Mumbai/City.txt 129 | ``` 130 | 131 | To add a content to a file with cat(redirect) . Run **`cat > /path/to/`** command 132 | ``` 133 | $ cat > Africa/Egypt/Cairo/City.txt 134 | Cairo 135 | `Type Ctrl + d from keyboard` 136 | ``` 137 | 138 | To create an empty file. Run **`touch /path/to/filename`** command 139 | ``` 140 | $ touch /home/michael/Asia/China/Country.txt 141 | ``` 142 | 143 | To see the content of a file in a scrollable manner. Run **`more /path/to/filename`** command <-- not recommended for large files 144 | ``` 145 | $ more new_file.txt 146 | ``` 147 | 148 | To see the content of a file and navigate throught the file. Run **`less /path/to/filename`** command 149 | ``` 150 | $ less new_file.txt 151 | ``` 152 | 153 | To get the long list of files and directories. Run **`ls -l`** command 154 | ``` 155 | $ ls -l 156 | ``` 157 | 158 | To list all files including the hidden. Run **`ls -la`** command 159 | ``` 160 | $ ls -a 161 | ``` 162 | 163 | To list all the files in the order they were modified. Run **`ls -lt`** command 164 | ``` 165 | $ ls -lt 166 | ``` 167 | 168 | To list all the files form oldest to newest. Run **`ls -ltr`** command 169 | ``` 170 | $ ls -ltr 171 | ``` 172 | -------------------------------------------------------------------------------- /Shell script/basic-operations.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | 3 | read -p "Enter No :" a 4 | read -p "Enter No :" b 5 | 6 | echo "Adding $a and $b is $(($a+$b))" 7 | echo "Subtracting $a and is $b $(($a-$b))" 8 | echo "Multiply $a and $b is $(($a*$b))" 9 | echo "Divide $a and $b is $(($a/$b))" 10 | 11 | echo "Exponate $a and $b is $(($a**$b))" 12 | echo "Reminder $a and $b is $(($a%$b))" 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Shell script/equal or not.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | read -p "Enter No1:" a 4 | read -p "Enter No2:" b 5 | 6 | if (($a==$b)); 7 | then 8 | echo $a is equal to $b 9 | else 10 | echo "$a is not equal to $b" 11 | fi 12 | -------------------------------------------------------------------------------- /Shell script/factorial.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | read -p "Enter a Number:" a 4 | b=1 5 | while (($a>1)); 6 | do 7 | b=$(($a*$b)) 8 | ((--a)) 9 | done 10 | 11 | echo "factorial of $a id $b" 12 | -------------------------------------------------------------------------------- /Shell script/select_pro.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | 4 | 5 | select drink in tea coffee water juice apple 6 | do 7 | case $drink in 8 | tea|coffee|water|juice) 9 | echo "Go to Canteen" 10 | ;; 11 | apple) 12 | echo "Available at store" 13 | ;; 14 | *) 15 | echo "invalid" 16 | break 17 | ;; 18 | esac 19 | done 20 | 21 | -------------------------------------------------------------------------------- /Shell script/sum ofemployees.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | read -p "Enter the no of Employee :" no 4 | sum=0 5 | for (( i=1 ; i<=$no; i++)); 6 | do 7 | read -p "Enter the salary of employee $i is :" a 8 | sum=$(($sum+$i)) 9 | done 10 | 11 | echo "Total Salary is $sum" 12 | --------------------------------------------------------------------------------