- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
服务器将其 PID 发送到客户端,客户端在循环中获取并发送 SIGUSR1 信号,因此我在服务器代码中有处理程序。从理论上讲,它需要获取信号并为客户端创建一些随机矩阵。客户端接收服务器 PID 但由于某种原因它从不向客户端发送矩阵。它等待并永远插入闪烁。我假设它们以某种方式不相互连接,但为什么呢?我尝试发送一个矩阵,结果很好。
客户端.c
int main()
{
int n=2;
pid_t pid=1;
int i,j;
double matrix[20][20];
char *myfifo2 = "/tmp/kek_fifo";
int server_to_client;
char *myfifo3 = "/tmp/fifor_fifo";
int fifor;
int forke=1;
char str[BUFSIZ];
fifor = open(myfifo3, O_RDONLY);
read(fifor,&pid,sizeof(pid));
printf("pid %d\n",pid);
close(fifor);
unlink(myfifo3);
while(1){
printf("no\n");
server_to_client = open(myfifo2, O_RDONLY);
read(server_to_client,matrix,sizeof(matrix));
close(server_to_client);
printf("yes\n");
forke=fork();
if(forke){
printf("forke buyuktur >= parent olmali %d \n",forke);
for(i = 0; i < 2*n; i++){
for(j = 0; j < 2*n; j++){
printf("%2f ",matrix[i][j]);
}
printf("\n");
}
wait(0); //parent child bekliyo
}
else{
printf("forke =0 yani child %d \n",forke);
exit(0);
}
//sleep(5);
kill(pid,SIGUSR1);
}
return 0;
}
服务器.c
void actionHandler(int signum);
void matrix_init(double m[20][20],int n);
void randome(double myPointer[20][20],int n);
static int signalcheck=0;
int main()
{
int i,j;
int n=2; //argv olacak
pid_t pide=getpid();
printf("pide is %d \n",pide);
struct sigaction action;
action.sa_handler=actionHandler;
action.sa_flags = 0;
int client_to_server;
char *myfifo = "/tmp/client_to_server_fifo";
int forke;
int server_to_client;
char *myfifo2 = "/tmp/kek_fifo";
char *myfifo3 = "/tmp/fifor_fifo";
int fifor;
char buf[BUFSIZ];
double M[20][20];
matrix_init(M,n);
/* create the FIFO (named pipe) */
// mkfifo(myfifo, 0666);
mkfifo(myfifo2, 0666);
mkfifo(myfifo3,0666);
fifor =open(myfifo3,O_WRONLY);
write(fifor,&pide,sizeof(pide));
close(fifor);
unlink(myfifo3);
if ((sigemptyset(&action.sa_mask) == -1) ||
(sigaction(SIGUSR1, &action, NULL) == -1))
perror("Failed to install SIGURS1 signal handler");
/* open, read, and display the message from the FIFO */
// client_to_server = open(myfifo, O_RDONLY);
printf("Server ON.\n");
while (1)
{
if(signalcheck==1){
forke=fork();
if(forke==0){
while(1){
randome(M,n);
server_to_client = open(myfifo2, O_WRONLY);
write(server_to_client,M,sizeof(M));
close(server_to_client);
unlink(myfifo2);
printf("matrix yarat\n");
//sleep(5);
//create matrix send to fifo
}
}
/*
read(client_to_server, buf, BUFSIZ);
if (strcmp("exit",buf)==0)
{
printf("Server OFF.\n");
break;
}
else if (strcmp("",buf)!=0)
{
printf("Received: %s\n", buf);
printf("Sending back...\n");
write(server_to_client,buf,BUFSIZ);
}
clean buf from any data
memset(buf, 0, sizeof(buf)); */
//sleep(5);
}
signalcheck=0;
}
// close(client_to_server);
// close(server_to_client);
// unlink(myfifo);
// unlink(myfifo2);
return 0;
}
void actionHandler(int signum)
{
signalcheck=1;
if(signum==SIGUSR1)
printf("i catched signal code from client i guess \n");
/* Signal handler code goes here. */
}
void matrix_init(double m[20][20],int z){
/*
this function purpose to create
2Nx2N int matrix n being int z
then initing this matrix to 0 before random numbers take in part
it has error checks .
blah!*/
int i, j,n=z;
double **k = (double **) malloc(n * sizeof(double*));
if(m == NULL){
printf("Error full matrix not created");
exit(-1);
}
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
m[i][j] = 0;
}
}
}
void randome(double myPointer[20][20],int n){
int i,j;
int r;
srand ( time(NULL) );
for(i=0;i<n*2;i++)
for(j=0;j<n*2;j++)
myPointer[i][j]=rand()%10;
}
最佳答案
我认为您的问题被忽视这么久的一个原因是代码中有相当多的代码和相当多的评论是不相关的代码,而且布局并不整洁。这些加起来就是“阅读代码是一项艰苦的工作”,这让人们无法真正阅读代码。
此代码似乎在运行 macOS Sierra 10.12.4 的 MacBook Pro 上使用 GCC 6.3.0 进行编译时运行正常。
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void)
{
int n = 2;
pid_t pid = 1;
int i, j;
double matrix[20][20];
char *myfifo2 = "/tmp/kek_fifo";
int server_to_client;
char *myfifo3 = "/tmp/fifor_fifo";
int fifor;
int forke = 1;
fifor = open(myfifo3, O_RDONLY);
read(fifor, &pid, sizeof(pid));
printf("pid %d\n", pid);
close(fifor);
unlink(myfifo3);
for (int loopnum = 0; loopnum < 20; loopnum++)
{
printf("begin loop %d\n", loopnum);
kill(pid, SIGUSR1);
printf("signal sent\n");
server_to_client = open(myfifo2, O_RDONLY);
printf("Open done\n");
read(server_to_client, matrix, sizeof(matrix));
printf("read done\n");
close(server_to_client);
printf("close done\n");
forke = fork();
if (forke)
{
printf("forke buyuktur >= parent olmali %d\n", forke);
for (i = 0; i < 2 * n; i++)
{
for (j = 0; j < 2 * n; j++)
{
printf("%2f ", matrix[i][j]);
}
printf("\n");
}
wait(0); // parent child bekliyo
}
else
{
printf("forke = 0 yani child %d\n", forke);
exit(loopnum);
}
}
return 0;
}
#include <assert.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
void actionHandler(int signum);
void matrix_init(double m[20][20], int n);
void randome(double myPointer[20][20], int n);
static volatile sig_atomic_t signalcheck = 0;
int main(void)
{
int n = 2; // argv olacak
pid_t pide = getpid();
printf("pide is %d\n", pide);
struct sigaction action;
action.sa_handler = actionHandler;
action.sa_flags = 0;
int forke;
int server_to_client;
char *myfifo2 = "/tmp/kek_fifo";
char *myfifo3 = "/tmp/fifor_fifo";
int fifor;
double M[20][20];
matrix_init(M, n);
srand(time(NULL));
if ((sigemptyset(&action.sa_mask) == -1) ||
(sigaction(SIGUSR1, &action, NULL) == -1))
{
perror("Failed to install SIGURS1 signal handler");
return 1;
}
/* create the FIFO (named pipe) */
mkfifo(myfifo2, 0666);
mkfifo(myfifo3, 0666);
fifor = open(myfifo3, O_WRONLY);
write(fifor, &pide, sizeof(pide));
close(fifor);
unlink(myfifo3);
printf("Server ON (%d).\n", signalcheck);
while (1)
{
if (signalcheck == 1)
{
printf("Signal received:\n");
signalcheck = 0;
randome(M, n);
forke = fork();
if (forke == 0)
{
server_to_client = open(myfifo2, O_WRONLY);
write(server_to_client, M, sizeof(M));
close(server_to_client);
printf("matrix yarat %d\n", (int)getpid());
exit(0);
}
else
{
int corpse;
int status;
while ((corpse = wait(&status)) != -1)
printf("PID %d exited with status 0x%.4x\n", corpse, status);
}
}
}
return 0;
}
void actionHandler(int signum)
{
static char msg[] = "I caught a signal from the client\n";
signalcheck = 1;
if (signum == SIGUSR1)
{
int nb = write(1, msg, sizeof(msg)-1);
assert(nb == sizeof(msg)-1);
}
}
void matrix_init(double m[20][20], int z)
{
for (int i = 0; i < z; i++)
{
for (int j = 0; j < z; j++)
m[i][j] = 0;
}
}
void randome(double myPointer[20][20], int n)
{
int i, j;
for (i = 0; i < n * 2; i++)
{
for (j = 0; j < n * 2; j++)
myPointer[i][j] = rand() % 10;
}
}
pide is 474
Server ON (0).
I caught a signal from the client
Signal received:
matrix yarat 476
PID 476 exited with status 0x0000
I caught a signal from the client
Signal received:
matrix yarat 478
PID 478 exited with status 0x0000
I caught a signal from the client
Signal received:
matrix yarat 481
PID 481 exited with status 0x0000
I caught a signal from the client
Signal received:
matrix yarat 483
PID 483 exited with status 0x0000
I caught a signal from the client
Signal received:
matrix yarat 485
PID 485 exited with status 0x0000
I caught a signal from the client
Signal received:
matrix yarat 488
PID 488 exited with status 0x0000
I caught a signal from the client
Signal received:
matrix yarat 491
PID 491 exited with status 0x0000
I caught a signal from the client
Signal received:
matrix yarat 493
PID 493 exited with status 0x0000
I caught a signal from the client
Signal received:
matrix yarat 496
PID 496 exited with status 0x0000
I caught a signal from the client
Signal received:
matrix yarat 499
PID 499 exited with status 0x0000
I caught a signal from the client
Signal received:
matrix yarat 501
PID 501 exited with status 0x0000
I caught a signal from the client
Signal received:
matrix yarat 503
PID 503 exited with status 0x0000
I caught a signal from the client
Signal received:
matrix yarat 506
PID 506 exited with status 0x0000
I caught a signal from the client
Signal received:
matrix yarat 512
PID 512 exited with status 0x0000
I caught a signal from the client
Signal received:
matrix yarat 514
PID 514 exited with status 0x0000
I caught a signal from the client
Signal received:
matrix yarat 516
PID 516 exited with status 0x0000
I caught a signal from the client
Signal received:
matrix yarat 518
PID 518 exited with status 0x0000
I caught a signal from the client
Signal received:
matrix yarat 521
PID 521 exited with status 0x0000
I caught a signal from the client
Signal received:
matrix yarat 523
PID 523 exited with status 0x0000
I caught a signal from the client
Signal received:
matrix yarat 525
PID 525 exited with status 0x0000
^C
^C
是我在终端窗口中中断服务器的地方。该程序在一个繁忙的循环中旋转,使用了 99.5% 的 CPU。
pid 474
begin loop 0
signal sent
Open done
read done
close done
forke buyuktur >= parent olmali 477
5.000000 6.000000 0.000000 9.000000
2.000000 1.000000 4.000000 7.000000
0.000000 8.000000 2.000000 5.000000
7.000000 8.000000 3.000000 2.000000
forke = 0 yani child 0
begin loop 1
signal sent
Open done
read done
close done
forke buyuktur >= parent olmali 480
0.000000 7.000000 0.000000 3.000000
0.000000 7.000000 2.000000 9.000000
2.000000 4.000000 5.000000 5.000000
9.000000 7.000000 3.000000 4.000000
forke = 0 yani child 0
begin loop 2
signal sent
Open done
read done
close done
forke buyuktur >= parent olmali 482
8.000000 6.000000 1.000000 4.000000
9.000000 2.000000 3.000000 9.000000
2.000000 3.000000 8.000000 0.000000
7.000000 8.000000 9.000000 8.000000
forke = 0 yani child 0
begin loop 3
signal sent
Open done
read done
close done
forke buyuktur >= parent olmali 484
1.000000 4.000000 2.000000 9.000000
5.000000 3.000000 5.000000 0.000000
5.000000 9.000000 7.000000 5.000000
3.000000 1.000000 7.000000 3.000000
forke = 0 yani child 0
begin loop 4
signal sent
Open done
read done
close done
forke buyuktur >= parent olmali 487
2.000000 3.000000 0.000000 4.000000
0.000000 2.000000 6.000000 8.000000
1.000000 8.000000 8.000000 7.000000
7.000000 4.000000 1.000000 4.000000
forke = 0 yani child 0
begin loop 5
signal sent
Open done
read done
close done
forke buyuktur >= parent olmali 489
6.000000 3.000000 4.000000 0.000000
5.000000 9.000000 2.000000 3.000000
9.000000 7.000000 5.000000 4.000000
5.000000 9.000000 1.000000 8.000000
forke = 0 yani child 0
begin loop 6
signal sent
Open done
read done
close done
forke buyuktur >= parent olmali 492
7.000000 1.000000 4.000000 6.000000
7.000000 1.000000 2.000000 4.000000
5.000000 1.000000 8.000000 6.000000
1.000000 9.000000 8.000000 6.000000
forke = 0 yani child 0
begin loop 7
signal sent
Open done
read done
close done
forke buyuktur >= parent olmali 495
0.000000 0.000000 5.000000 1.000000
6.000000 8.000000 3.000000 1.000000
8.000000 1.000000 6.000000 9.000000
7.000000 3.000000 1.000000 5.000000
forke = 0 yani child 0
begin loop 8
signal sent
Open done
read done
close done
forke buyuktur >= parent olmali 497
3.000000 0.000000 1.000000 8.000000
4.000000 4.000000 2.000000 3.000000
8.000000 9.000000 6.000000 3.000000
1.000000 3.000000 6.000000 3.000000
forke = 0 yani child 0
begin loop 9
signal sent
Open done
read done
close done
forke buyuktur >= parent olmali 500
4.000000 0.000000 9.000000 3.000000
1.000000 5.000000 1.000000 2.000000
3.000000 0.000000 5.000000 6.000000
4.000000 5.000000 9.000000 7.000000
forke = 0 yani child 0
begin loop 10
signal sent
Open done
read done
close done
forke buyuktur >= parent olmali 502
4.000000 9.000000 7.000000 6.000000
6.000000 4.000000 7.000000 1.000000
3.000000 5.000000 9.000000 0.000000
7.000000 9.000000 7.000000 9.000000
forke = 0 yani child 0
begin loop 11
signal sent
Open done
read done
close done
forke buyuktur >= parent olmali 504
3.000000 3.000000 0.000000 3.000000
7.000000 5.000000 8.000000 5.000000
6.000000 1.000000 3.000000 6.000000
8.000000 3.000000 3.000000 0.000000
forke = 0 yani child 0
begin loop 12
signal sent
Open done
read done
close done
forke buyuktur >= parent olmali 507
6.000000 5.000000 1.000000 8.000000
7.000000 3.000000 9.000000 7.000000
9.000000 4.000000 0.000000 5.000000
1.000000 6.000000 9.000000 3.000000
forke = 0 yani child 0
begin loop 13
signal sent
Open done
read done
close done
forke buyuktur >= parent olmali 513
6.000000 4.000000 6.000000 3.000000
5.000000 6.000000 6.000000 0.000000
2.000000 1.000000 5.000000 2.000000
7.000000 3.000000 9.000000 8.000000
forke = 0 yani child 0
begin loop 14
signal sent
Open done
read done
close done
forke buyuktur >= parent olmali 515
0.000000 3.000000 7.000000 8.000000
9.000000 1.000000 2.000000 4.000000
5.000000 7.000000 4.000000 8.000000
3.000000 1.000000 1.000000 6.000000
forke = 0 yani child 0
begin loop 15
signal sent
Open done
read done
close done
forke buyuktur >= parent olmali 517
0.000000 8.000000 9.000000 7.000000
7.000000 3.000000 3.000000 0.000000
4.000000 0.000000 8.000000 1.000000
5.000000 8.000000 4.000000 4.000000
forke = 0 yani child 0
begin loop 16
signal sent
Open done
read done
close done
forke buyuktur >= parent olmali 520
8.000000 3.000000 8.000000 8.000000
1.000000 7.000000 0.000000 3.000000
4.000000 2.000000 0.000000 3.000000
8.000000 4.000000 4.000000 8.000000
forke = 0 yani child 0
begin loop 17
signal sent
Open done
read done
close done
forke buyuktur >= parent olmali 522
9.000000 3.000000 9.000000 3.000000
3.000000 3.000000 2.000000 9.000000
9.000000 3.000000 6.000000 0.000000
8.000000 0.000000 8.000000 1.000000
forke = 0 yani child 0
begin loop 18
signal sent
Open done
read done
close done
forke buyuktur >= parent olmali 524
9.000000 1.000000 4.000000 3.000000
4.000000 8.000000 9.000000 2.000000
2.000000 5.000000 0.000000 6.000000
9.000000 0.000000 5.000000 7.000000
forke = 0 yani child 0
begin loop 19
signal sent
Open done
read done
close done
forke buyuktur >= parent olmali 526
2.000000 0.000000 5.000000 3.000000
1.000000 2.000000 1.000000 3.000000
3.000000 6.000000 5.000000 8.000000
7.000000 3.000000 2.000000 5.000000
forke = 0 yani child 0
请注意,客户端在 20 个循环后整齐地退出;它可能应该更小。
srand()
once signalcheck
设置为零,除非它是一。一个问题似乎是每次检查时“signalcheck”变量都为零。static volatile sig_atomic_t signalcheck
。fork()
之前生成随机矩阵,以便看到不同的结果。printf()
.代码远非完美。有很多事情需要解决,包括:
fork()
之后)。关于c - While 循环中的 FIFO 卡住并等待读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43441561/
我试图让脚本暂停大约 1 秒,然后继续执行脚本,但我似乎无法弄清楚如何做。这是我的代码: function hello() { alert("Hi!") //I need about a 1
wait() 和 wait(timeout) 之间有什么区别。无论如何 wait() 需要等待通知调用,但为什么我们有 wait(timeout)? 那么 sleep(timeout) 和 wait(
我需要做什么: 我有一个带有文件输入和隐藏文本输入的上传表单。用户上传图像,图像被操作,然后发送到远程服务器进行处理,这需要几秒钟,然后远程服务器将最终的图像发送回家庭服务器,并保存在新文件夹中。 J
大家好,我正在使用 Visual C++ 2010,尝试使用 Winsock 编写服务器/客户端应用程序...我不确定为什么,但有时服务器会在 listen() 函数处等待,有时会在 accept 处
任务描述 我为我的 Angular 应用程序实现了 CRSF 保护。服务器检查 crsf token 是否位于请求的 header “X-CSRF-TOKEN”中。如果不是,它会发送一个 HTTP 响
我想做这个例子https://stackoverflow.com/a/33585993/1973680同步。 这是正确的实现方式吗? let times= async (n,f)=>{
我如何将 while 循环延迟到 1 秒间隔,而不会将其运行的整个代码/计算机的速度减慢到一秒延迟(只是一个小循环)。 最佳答案 Thread.sleep(1000); // do nothing f
我知道这是一个重复的问题。但是我无法通过解释来理解。我想用一个很好的例子来清楚地理解它。任何人都可以帮忙吗。 “为什么我们从同步上下文中调用 wait()、notify() 方法”。 最佳答案 当我们
我有一个 click 事件,该事件是第一次从另一个地方自动触发的。我的问题是它运行得太快,因为所需的变量仍在由 Flash 和 Web 服务定义。所以现在我有: (function ($) {
我有如下功能 function async populateInventories(custID){ this.inventories = await this.inventoryServic
我一直对“然后”不被等待的行为感到困扰,我明白其原因。然而,我仍然需要绕过它。这是我的用例。 doWork(family) { return doWork1(family)
我想我理解异步背后的想法,返回一个Future,但是我不清楚异步在一个非常基本的层面上如何表现。据我了解,它不会自动在程序中创建异步行为。例如: import 'dart:async'; main()
我正在制作一个使用异步的Flutter应用程序,但它的工作方式不像我对它的了解。所以我对异步和在 Dart 中等待有一些疑问。这是一个例子: Future someFunction() async {
我在 main.tf 中创建资源组和 vNet,并在同一文件中引用模块。问题是,模块无法从模块访问这些资源。相关代码(删除了大部分代码,只留下相关部分): main.tf: module "worke
我的代码的问题是,当代码第一次运行时,我试图获取的 dom 元素并不总是存在,如果它不存在,那么永远不会做出 promise 。 我是否可以等到 promise 做出后再尝试实现它? 我希望我的最后一
所以,过去几天我一直在研究这段代码,并尝试实现回调/等待/任何需要的东西,但没有成功。 问题是,我如何等待响应,直到我得到两个函数的回调? (以及我将如何实现) 简而言之,我想做的是: POST 发生
谁能帮我理解这一点吗? 如果我们有一个类: public class Sample{ public synchronized method1(){ //Line1 .... wait();
这是我编写的代码,用于测试 wait() 和 notify() 的工作。现在我有很多疑问。 class A extends Thread { public void run() { try
我有以下代码由于语法错误而无法运行(在异步函数外等待) 如何使用 await 定义变量并将其导出? 当我这样定义一个变量并从其他文件导入它时,该变量是只创建一次(第一次读取文件时?)还是每次导入时都创
一个简单的线程程序,其中写入器将内容放入堆栈,读取器从堆栈中弹出。 java.util.Stack; import java.util.concurrent.ExecutorService; impo
我是一名优秀的程序员,十分优秀!