static char *TCPCommand; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; int SetUpTCPtoSendInformation(char option[], char **command) { static int isFirstCall = 1; pthread_mutex_lock(&mutex); // 加锁 if (isFirstCall && strstr(option, "set")) { if (TCPCommand != NULL) { free(TCPCommand); } TCPCommand = malloc(strlen(*command) + 1); if (TCPCommand == NULL) { printf("Failed to set and obtain TCP command variable memory allocation\n"); goto fail; } strcpy(TCPCommand, *command); printf("set:%s\n", TCPCommand); isFirstCall = 0; goto succeed; } else if (!isFirstCall && strstr(option, "get") && TCPCommand != NULL && strlen(TCPCommand)) { free(*command); *command = malloc(strlen(TCPCommand) + 1); strcpy(*command, TCPCommand); printf("get:%s\n", *command); memset(TCPCommand, '\0', strlen(TCPCommand)); free(TCPCommand); TCPCommand = NULL; isFirstCall = 1; goto succeed; }else { *command = malloc(1); if (*command == NULL) { CON_LOG("Failed to allocate memory for command\n"); goto fail; } **command = '\0'; CON_LOG("Invalid option\n"); goto fail; } fail: pthread_mutex_unlock(&mutex); // 解锁 return 0; succeed: pthread_mutex_unlock(&mutex); // 解锁 return 1; }两个进程一个进程是 char* command1 = "echo `uci get m_system.usesiminfo.operator;uci get m_system.usesiminfo.card_slot; \ uci get m_system.usesiminfo.iccid;uci get m_system.usesiminfo.lock;uci get m_system.system.simnum`"; char* command2 = "echo `uci get m_system.sim1.operator;uci get m_system.sim1.card_slot;uci get m_system.sim1.iccid; \ uci get m_system.sim2.operator;uci get m_system.sim2.card_slot;uci get m_system.sim2.iccid; \ uci get m_system.sim3.operator;uci get m_system.sim3.card_slot;uci get m_system.sim3.iccid; \ uci get m_system.sim4.operator;uci get m_system.sim4.card_slot;uci get m_system.sim4.iccid`"; SetUpTCPtoSendInformation(setExeCmd,&command1);SetUpTCPtoSendInformation(setExeCmd,&command2);另一个是whlie(1){SetUpTCPtoSendInformation("get", &SendString);}请问同时运行这两个进程get能获取到这两个set的值,如果娶不到请帮我优化程序
时间: 2024-04-15 22:27:12 浏览: 142
pthread_cond_wait() 用法深入分析
根据给出的代码,同时运行这两个进程时,第一个进程执行`SetUpTCPtoSendInformation(setExeCmd, &command1)`会设置TCPCommand的值,第二个进程执行`SetUpTCPtoSendInformation(setExeCmd, &command2)`也会设置TCPCommand的值。但是,在第二个进程执行`SetUpTCPtoSendInformation("get", &SendString)`时,由于第一个进程已经将TCPCommand的值释放并设为NULL,因此第二个进程无法获取到第一个进程设置的值。
为了优化程序,可以使用线程间共享的全局变量来存储TCPCommand的值,而不是在`SetUpTCPtoSendInformation`函数内部使用静态变量。这样,两个进程就可以共享该全局变量的值。
以下是一个修改后的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
static char *TCPCommand = NULL;
static pthread_mutex_t mutex = P_MUTEX_INITIALIZER;
int SetTCPCommand(char *command) {
pthread_mutex_lock(&mutex); // 加锁
if (TCPCommand != NULL) {
free(TCPCommand);
}
TCPCommand = malloc(strlen(command) + 1);
if (TCPCommand == NULL) {
printf("Failed to allocate memory for TCP command\n");
pthread_mutex_unlock(&mutex); // 解锁
return 0;
}
strcpy(TCPCommand, command);
printf("set: %s\n", TCPCommand);
pthread_mutex_unlock(&mutex); // 解锁
return 1;
}
int GetTCPCommand(char **command) {
pthread_mutex_lock(&mutex); // 加锁
if (TCPCommand == NULL || strlen(TCPCommand) == 0) {
printf("TCP command is empty\n");
pthread_mutex_unlock(&mutex); // 解锁
return 0;
}
*command = malloc(strlen(TCPCommand) + 1);
if (*command == NULL) {
printf("Failed to allocate memory for command\n");
pthread_mutex_unlock(&mutex); // 解锁
return 0;
}
strcpy(*command, TCPCommand);
printf("get: %s\n", *command);
free(TCPCommand);
TCPCommand = NULL;
pthread_mutex_unlock(&mutex); // 解锁
return 1;
}
```
同时运行这两个进程时,第一个进程可以调用`SetTCPCommand`函数设置TCPCommand的值,第二个进程可以调用`GetTCPCommand`函数获取到第一个进程设置的值。
希望这个优化后的代码对你有帮助!如果你还有其他问题,请随时提问。
阅读全文