Skip to content

Commit

Permalink
Use nanosleep instead of sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
sax committed Jun 27, 2024
1 parent fb93277 commit 3759d39
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions distrans.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
Expand All @@ -12,7 +13,7 @@

void implode(int socket) {
close(socket);
exit(1);
exit(0);
}

void forbidden(int socket) {
Expand Down Expand Up @@ -73,7 +74,10 @@ void respond(int socket, int verbose) {
(void)sprintf(write_buffer,"%s", GIF);
(void)write(socket, write_buffer, strlen(write_buffer));

sleep(0.001);
struct timespec ts;
ts.tv_sec = 1 / 1000;

nanosleep(&ts, &ts);
implode(socket);
}

Expand Down Expand Up @@ -122,7 +126,7 @@ int main(int argc, char *argv[]) {
socketfd = accept(listenfd, (struct sockaddr*)&cli_addr, &length);

pid = fork();
if(pid == 0) {
if (pid == 0) {
(void)close(listenfd);
respond(socketfd, verbose);
} else if (pid > 0) {
Expand Down

0 comments on commit 3759d39

Please sign in to comment.