由于事务方法中的第一个
sscanf
语句,我遇到了分段错误。我不确定为什么会发生这种情况。我尝试更改 sscanf
语句和变量,但这没有帮助。我在调试方面没有太多经验,但是在执行事务方法中的第一个 sscanf
语句后,代码停止并变成亮红色,并且其余代码将不会在调试器中运行。当我尝试在 Visual Studio 代码中运行没有调试器的代码时,它运行时,输出不正确,因为 assignment_5_input.txt 的内容是:
A1 balance 5000
A2 balance 5000
A3 balance 5000
A4 balance 5000
A5 balance 5000
C1 deposit A2 1000
C2 withdraw A1 300 deposit A4 200
C3 deposit A3 500 withdraw A4 400 withdraw A1 100
C4 withdraw A1 40000 withdraw A2 800
C5 withdraw A5 5000
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define max_lines 100
#define max_len 1000
struct ThreadArgs {
char *data;
int *account;
};
//mutex to control thread execution
pthread_mutex_t lock;
void *transaction(void *arg){
struct ThreadArgs *args = (struct ThreadArgs *)arg;
char *data_array = args->data;
int *account_array = args->account;
int deposit = 0;
int withdraw = 0;
int acc_num = 0;
char wordd[] = "deposit";
char wordw[] = "withdraw";
char ddata[strlen(data_array) + 1];
strcpy(ddata, data_array);
printf("%s", ddata);
pthread_mutex_lock(&lock); // ENTRY
if (strstr(ddata, wordd) != NULL) {
sscanf(ddata, "deposit A%d %d", &acc_num, &deposit);
account_array[acc_num - 1] += deposit;
printf("Deposit: %d\n", deposit);
printf("Account Number: %d\n", acc_num);
}
if (strstr(ddata, wordw) != NULL) {
//sscanf(ddata, "withdraw A%d %d", &acc_num, &withdraw);
if (account_array[acc_num - 1] - withdraw >= 0) {
account_array[acc_num - 1] -= withdraw;
printf("Withdrawal: %d\n", withdraw);
printf("Account Number: %d\n", acc_num);
}
}
free(args);
pthread_mutex_unlock(&lock); // EXIT
return NULL;
}
int main() {
//variables
int line = 0;
char data[max_lines][max_len];
int num_account = 0;
int num_client = 0;
int num_account_count = 0;
int num_client_count = 0;
//opening file
FILE *fp;
fp = fopen("assignment_5_input.txt", "r");
if (fp == NULL){
printf("File could not open.\n");
return 1;
}
while (!feof(fp) && !ferror(fp)){
if (fgets(data[line], max_len, fp) != NULL){
line++;
}
}
fclose(fp);
for (int i = 0; i < line; i++){
if (sscanf(data[i], "A%d balance ", &num_account)){
num_account_count++;
}
if (sscanf(data[i], "C%d ", &num_client)){
num_client_count++;
}
}
//account info
int account[num_account_count];
//putting balance in for each account
for (int i = 0; i < num_account_count; i++) {
int balance = 0;
sscanf(data[i], "A%d balance %d\n", &num_account, &balance);
account[num_account - 1] = balance;
}
//client info
pthread_mutex_init(&lock, NULL);
pthread_t threads[num_client_count];
for (int i = num_account_count; i < line; i++) {
struct ThreadArgs *args = malloc(sizeof(struct ThreadArgs));
args->data = data[i];
args->account = account;
pthread_create(&threads[i], NULL, transaction, (void *)args);
}
for (int i = 0; i < num_client; i++){
pthread_join(threads[i], NULL);
}
pthread_mutex_destroy(&lock);
printf("No. of Accounts: %d\n", num_account);
printf("No. of Clients: %d\n", num_client);
for (int i = 0; i < num_account_count; i++) {
printf("A%d balance: %d\n", i+1, account[i]);
}
return 0;
}
这条线是麻烦的根源:
account[num_account - 1] = balance;
注意不使用循环变量,
i
。因此,稍后,该数组元素的未初始化值被传递到 transaction()
函数中,导致段错误的可能原因。