• Nov 19, 2022 •CodeCatch
0 likes • 0 views
#include <stdio.h> #include <string.h> #include <unistd.h> #define READ 0 /* The index of the read end of the pipe */ #define WRITE 1 /* The index of the write end of the pipe */ char * phrase = "This goes in the pipe"; int main() { int fd[2], bytesRead; char message[100]; /* Parent process' message buffer */ pipe(fd); /* Create unnamed pipe */ if (fork() == 0) /* Child, writer */ { close(fd[READ]); /* Close unused end */ write(fd[WRITE], phrase, strlen(phrase) + 1); /* Include NULL */ close(fd[WRITE]); /* Close used end */ } else /* Parent, reader */ { close(fd[WRITE]); /* Close unused end */ bytesRead = read(fd[READ], message, 100); printf("Parent just read %i bytes: %s\n", bytesRead, message); close(fd[READ]); /* Close used end */ } }
#include <stdio.h> #include <assert.h> #include <signal.h> void myHandler(int iSig) { printf("In myHandler with argument %d\n", iSig); } int main() { void( * pfRet)(int) = signal(SIGINT, myHandler); assert(pfRet != SIG_ERR); printf("Entering an infinite loop\n"); while (1) { printf("."); } return 0; // use CTRL+\ to exit }
0 likes • 2 views
#include <stdio.h> #include <sys/types.h> #include <unistd.h> int glbvar = 6; int main() { int locvar = 88; pid_t pid; printf("Before fork()\n"); if ((pid = fork()) == 0) { /* child */ glbvar++; locvar++; } else if (pid > 0) { /* parent */ sleep(2); } else perror("fork error"); printf("pid=%d, glbvar=%d, locvar=%d\n", getpid(), glbvar, locvar); return 0; } /* end main */
• Aug 5, 2023 •LeifMessinger
// compile: gcc socketTCPClient.c -o socketTCPClient.out // usage : ./socketTCPClient.out port // Doesn't send anything, just connects and prints out the response from the server #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, portno; char recvBuff[1025]; struct sockaddr_in serv_addr; memset(recvBuff, '0', sizeof(recvBuff)); if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){ printf("socket error\n"); exit(EXIT_FAILURE); } serv_addr.sin_family = AF_INET; portno = atoi(argv[1]); serv_addr.sin_port = htons(portno); serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); if(connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0){ printf("connect error\n"); exit(EXIT_FAILURE); } while((n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0){ recvBuff[n] = 0; if (fputs(recvBuff, stdout) == EOF) { printf("fputs error\n"); } } if(n < 0){ printf("read error\n"); } return 0; }
#include <stdio.h> #include <unistd.h> int main() { char * cmd[] = { "who", "ls", "date" }; int i; printf("0=who 1=ls 2=date : "); scanf("%d", & i); execlp(cmd[i], cmd[i], (char * ) 0); printf("execlp failed\n"); return 0; }
// The following code is the fast inverse square root implementation from Quake III Arena // this code has been stripped of C preprocessor directives, but includes the exact original comment text float Q_rsqrt( float number ) { long i; float x2, y; const float threehalfs = 1.5F; x2 = number * 0.5F; y = number; i = * ( long * ) &y; // evil floating point bit level hacking i = 0x5f3759df - ( i >> 1 ); // what the fuck? y = * ( float * ) &i; y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed return y; }