Skip to main content

pipe() example

Nov 19, 2022CodeCatch
Loading...

More C Posts

inner fork() variables

Nov 19, 2022CodeCatch

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 */

Bitwise operators with pointers

Nov 18, 2022AustinLeath

0 likes • 0 views

#include <stdio.h>
#include <stdlib.h>
int main(){
int * int_ptr;
int_ptr = (int *)malloc(2*sizeof(int));
if(!int_ptr) {
printf("Something went wrong while allocating memory. Exiting...");
exit(1);
}
printf("Enter first integer: ");
scanf("%i", &int_ptr[0]);
printf("Enter second integer: ");
scanf("%i", &int_ptr[1]);
printf("Original values: 1st = %i 2nd = %i\n",int_ptr[0], int_ptr[1]);
int_ptr[0] = int_ptr[0] ^ int_ptr[1];
//printf("%i\n", int_ptr[0]);
int_ptr[1] = int_ptr[0] ^ int_ptr[1];
//printf("%i\n", int_ptr[1]);
int_ptr[0] = int_ptr[0] ^ int_ptr[1];
//printf("%i\n", int_ptr[0]);
printf("Swapped values: 1st = %i 2nd = %i\n", int_ptr[0], int_ptr[1]);
free(int_ptr);
exit(0);
}

signal() example

Nov 19, 2022CodeCatch

0 likes • 0 views

#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
}

fork() example

Nov 19, 2022CodeCatch

0 likes • 2 views

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
pid_t pid; /* could be int */
int i;
pid = fork();
printf("PID=%d\n", pid);
if (pid > 0) {
/* parent */
for (i = 0; i < 10; i++)
printf("\t\t\tPARENT %d\n", i);
} else {
/* child */
for (i = 0; i < 10; i++)
printf("CHILD %d\n", i);
}
return 0;
}

Fast inverse square root

Nov 19, 2022CodeCatch

0 likes • 0 views

// 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;
}

socketTCPClient.c

Aug 5, 2023LeifMessinger

0 likes • 0 views

// 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;
}