这个小代码我有问题.我有一个"服务器"和一个"客户端".服务器等待SIGUSR1客户端.但是当我发送SIGUSR1循环时,服务器不处理每个信号!我i++每次收到信号时都会这样做,而当我发送1000个信号时,我得到981.
usleep()并sleep()没有帮助.
这是客户端代码:
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
int main(int ac, char **av)
{
int i = 0;
int status = 0;
if (ac < 2)
return (0);
printf("kill %s (%d)", av[1], atoi(av[1]));
for (i=0;i<=1000;i++)
{
printf("%d\n", i);
kill(atoi(av[1]), SIGUSR1);
}
kill(atoi(av[1]), SIGUSR2);
}
Run Code Online (Sandbox Code Playgroud)
和服务器代码:
#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int i = 0;
void sig2(int signum)
{
/* usleep(30); */
printf("%d\n", i); …Run Code Online (Sandbox Code Playgroud) 我正在尝试ls用 C编写命令,但stat()拒绝打开任何其他目录。
~/Desktop/ls$ cat bug.c
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <unistd.h>
int main(int ac, char **av)
{
DIR *d;
struct dirent *dir;
struct stat file;
d = opendir(av[1]);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
printf("%s ->", dir->d_name);
if (lstat(dir->d_name, &file) < 0)
printf(" can't read file %s!", dir->d_name);
printf("\n");
}
}
closedir(d);
return (0);
}
Run Code Online (Sandbox Code Playgroud)
运行时./a.out。或任何子文件夹,它工作正常。但是如果我写./a.out .. …