- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我正在编写一个程序,它会迭代目录中的每个文件/文件夹,对于它遇到的每个 CSV 文件,它将 fork() 然后对其进行排序并输出 CSV 文件。
排序发生在 beginSort() 中,它对 CSV 运行合并排序,然后输出。
这是问题......当我在默认主父进程上运行 beginSort() 时,它完全按预期工作。然而,当它从 forked() 子进程运行时,代码在第 170 行之后立即失败:
fgets(titleRow.rowValue, 999, fp);
我无法理解正在发生的事情。当我在这一行之前设置断点时...GDB 仍然直接运行它,我无法获得有意义的信息。这是完整的 sorter.c 文件:
#include "Sorter.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
int isString(char* string) {
int i = 0;
int decimal = 0;
// empty string
if(string == NULL || string == ""){
return 1;
}
// goes character by character to check if its a number or string
while(i < strlen(string)){
if( !isdigit(string[i])){
if(string[i] == '.' && !decimal){
if(i == strlen(string) - 1 || !isdigit(string[i + 1])){
return 1;
}
decimal = 1;
i++;
continue;
}
return 1;
}
i++;
}
return 0;
}
// trims trailing and leading blank spaces in a string
char* removeWhitespace(char *string, int i) {
char *final;
while(isspace((unsigned char)*string)){
string++;
}
if(*string == 0) {
return string;
}
final = string + i;
while(final > string && isspace((unsigned char)*final)) {
final--;
}
*(final+1) = 0;
return string;
}
// splits row by commas and places them into the structs
char** customStrTok(char* line, int sortedColumnNum) {
int i = 0;
int j = 0;
int k = 0;
// stores resulting fields
char** result = (char**)malloc(sizeof(char*) * (sortedColumnNum + 1));
char* container = (char*)malloc(500);
// checks for quotation marks in string
int boolIsQuote = 0;
//go through each character
while(i < strlen(line)){
if(line[i] == '"' && boolIsQuote == 0){
boolIsQuote = 1;
}
else if(line[i] == '"' && boolIsQuote == 1){
//store value in result
result[k] = (char*) malloc((j + 1) * sizeof(char));
container = removeWhitespace(container, j - 1);
strcpy(result[k], container);
memset(&container[0], 0, strlen(container));
boolIsQuote = 0;
j = 0;
k++;
i++;
}
//splits row by columns
else if((line[i] == ',' || i == strlen(line) - 1) && boolIsQuote != 1){
//if there is no character; (eg: ,,)
if(!container){
container[0] = '\0';
}
if(i == strlen(line) - 1 && line[i] != '\n'){
container[j] = line[i];
j++;
}
// copy into result array
result[k] = (char*)malloc((j+1) * sizeof(char));
container = removeWhitespace(container, j - 1);
strcpy(result[k], container);
memset(&container[0], 0, strlen(container));
j = 0;
k++;
// if comma is at the end
if(line[i] == ',' && i == strlen(line) - 2){
container[0] = '\0';
result[k] = (char*)malloc((j+1) * sizeof(char));
strcpy(result[k], container);
memset(&container[0], 0, strlen(container));
}
} else{
//copy into container
if(j == 0){
if(line[i] == ' '){
i++;
continue;
}
}
container[j] = line[i];
j++;
}
i++;
}
i = 0;
return result;
}
void beginSort(char* selectedColumn, char* fileName){
printf("%s\n", "Begin sort called");
FILE* fp;
fp = fopen(fileName, "r");
row titleRow;
int sortedColumnNum = 1;
char *token;
// sets up the row of column titles
titleRow.rowValue = (char*) malloc (sizeof(char) * 1000);
printf("%s\n", "Program stops here when on forked process");
printf("I failed. This is my id, %d, and this is my parents id %d\n", getpid(), getppid());
fgets(titleRow.rowValue, 999, fp);
printf("%s\n", "Got passed fgets");
titleRow.rowLength = strlen(titleRow.rowValue);
titleRow.fields = (char**) malloc(sizeof(char *) * titleRow.rowLength);
token = strtok(titleRow.rowValue, ",");
titleRow.fields[0] = token;
//Beginning splitting the tokens and check if the column name entered exists
int selectedColumnExist = 0;
while((token = strtok(NULL, ","))){
titleRow.fields[sortedColumnNum] = token;
//This removes the last whitespace value (\n) because for the last column in the CSV, the check would fail
titleRow.fields[sortedColumnNum] = removeWhitespace(titleRow.fields[sortedColumnNum], strlen(titleRow.fields[sortedColumnNum]) - 1);
if (strcmp(titleRow.fields[sortedColumnNum], selectedColumn) == 0){
//the column exists
selectedColumnExist = 1;
}
sortedColumnNum++;
}
if (selectedColumnExist != 1){
printf("Sorry, the column you entered doesn't exist in the csv\n");
return;
}
titleRow.sortedColumnNum = sortedColumnNum;
int length = strlen(titleRow.fields[sortedColumnNum - 1]);
if(titleRow.fields[sortedColumnNum - 1][length - 1] == '\n'){
titleRow.fields[sortedColumnNum - 1][length - 2] = '\0';
}
// trim column titles
int i = 0;
while(i < sortedColumnNum){
titleRow.fields[i] = removeWhitespace(titleRow.fields[i], strlen(titleRow.fields[i]) - 1);
i++;
}
row *data;
int numberOfRows;
data = (row*) malloc (sizeof(row) * 15000); //size matters
// non title rows, aka all the other ones
row regularRow;
regularRow.rowValue = (char*) malloc (sizeof(char) * 1000);
int currentRow = 0;
while(fgets(regularRow.rowValue, 999, fp) != NULL){
regularRow.rowLength = strlen(regularRow.rowValue);
regularRow.fields = (char**) malloc(sizeof(char *) * (sortedColumnNum+1));
regularRow.fields = customStrTok(regularRow.rowValue, sortedColumnNum);
data[currentRow++] = regularRow;
}
numberOfRows = currentRow;
int columnToSort = 0;
while(columnToSort < titleRow.sortedColumnNum){
if(strcmp(titleRow.fields[columnToSort], selectedColumn) == 0){
break;
}
columnToSort++;
}
//Call mergesort
mergeSort(data, columnToSort, numberOfRows);
//Export to a new file
FILE *fp2;
char* filename2;
//make this work
//filename2= strcat(fileName, "-sorted-.csv");
filename2="result.csv";
printf("%s\n", "Begin export");
printf("%s\n", filename2);
fp2=fopen(filename2,"w+");
int vv,zz;
vv = 0;
while(vv < sortedColumnNum){
fprintf(fp2, titleRow.fields[vv]);
if(vv != sortedColumnNum - 1){
fprintf(fp2, ",");
}else{
fprintf(fp2, "\n");
}
vv++;
}
vv = 0;
zz = 0;
while(vv < numberOfRows){
while(zz < sortedColumnNum){
fprintf(fp2, data[vv].fields[zz]);
if(zz != sortedColumnNum - 1){
fprintf(fp2, ",");
}else{
fprintf(fp2, "\n");
}
zz++;
}
vv++;
zz = 0;
}
fclose(fp2);
}
void traverseDirectory(char* dirName, char* selectedColumn){
DIR *dir;
struct dirent *ent;
if ((dir = opendir (dirName)) != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
char* itemName = ent->d_name;
int length = strlen(itemName);
int pid;
//CSV FILE FOUND
if (length > 0 && itemName[length - 1] == 'v'
&& itemName[length - 2] == 's'
&& itemName[length - 3] == 'c'
&& itemName[length - 4] == '.' )
{
//confirm if valid csv file (opens correctly and has valid headers)
pid = fork();
printf("%d\n", pid);
printf("%s\n", "CSV found");
}
switch(pid){
case 0:
beginSort(itemName, selectedColumn);
break;
case -1:
printf("%s\n", "Error creating fork");
default:
continue;
}
return;
// printf("%d\n", strlen(itemName));
//printf ("%s\n", itemName);
}
closedir (dir);
} else {
/* could not open directory */
perror ("");
return;
}
//CODE TO OPEN DIRECTORY GIVEN GOES HERE
/*
* DIR* test = opendir(dirName);
* dirent* newfile = readdir(test);
*
*/
/*for (loop to iterate through everything in given dirName){
*
if (DIRECTORY){
pid = fork();
switch(pid){
case 0:
traverseDirectory(directoryName);
case -1:
error
default:
continue;
}
if (FILE){
if (currentFile == CSV && currentFile == validCSV file){
pid == fork();
}
switch(pid){
case 0:
beginSort(currentfile, selectedColumn)
break;
case -1:
error
default:
continue;
}
}
return;
}*/
}
int main (int argc, char* argv[]){
// column to sort by
char* selectedColumn;
if (strcmp(argv[1],"-c") != 0){
printf("Sorry, you must use the -c flag to declare a column\n");
return 1;
}
if (argc != 3 && argc != 5 && argc != 7){
printf("Invalid argument size\n");
return 1;
}
selectedColumn = argv[2];
//beginSort(selectedColumn, "movie_metadata.csv");
if (argc == 3){
traverseDirectory("./", selectedColumn);
}
if (argc == 5){
//check if argv[3] == -d
//{ do something }
//check if arv[3] == -o
//{ do something }
}
if (argc == 7){
//check if argv[3] == -d && argv[5] == -o
//
}
return 0;
}
有人有什么想法吗? GDB 似乎在程序实际失败的行之前报告了 seg 错误,所以我在这里感到困惑。
最佳答案
所以你的代码看起来有点像这样...我删除了与问题无关的行。
fp = fopen(fileName, "r");
// ....
fgets(titleRow.rowValue, 999, fp);
如果filename
不存在,您的代码会发生什么?您应该始终检查函数的返回值以确保它们有效。
您假设由于 traverseDirectory
找到了该文件,因此您可以打开它。但您忘记了 itemName
是相对于 dirName
的。您需要将两者结合起来,以获得完整的文件名才能打开它。
识别 CSV 文件的方式也存在问题。
if (length > 0 && itemName[length - 1] == 'v'
&& itemName[length - 2] == 's'
&& itemName[length - 3] == 'c'
&& itemName[length - 4] == '.' )
例如,如果 length
为 3,您最终将访问不正确的 itemName[-1]
。您希望确保长度至少为 5(除非名为“.csv”的文件有效?),并且您可以使用 strcmp
来更轻松地查看您在做什么。
if (length > 4 && strcmp(itemName+length-4,".csv")==0)
关于c - forked() 子进程上的 strcmp() 处出现 SIGSEGV 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46976637/
我是 Linux 的新手,并且继承了保持我们的单一 Linux 服务器运行的职责。这是我们的SVN服务器,所以比较重要。 原来在我之前维护它的人有一个 cron 任务,当有太多 svnserve 进程
Node 虽然自身存在多个线程,但是运行在 v8 上的 JavaScript 是单线程的。Node 的 child_process 模块用于创建子进程,我们可以通过子进程充分利用 CPU。范例:
Jenkins 有这么多进程处于事件状态是否正常? 我检查了我的设置,我只配置了 2 个“执行者”... htop http://d.pr/i/RZzG+ 最佳答案 您不仅要限制 Master 中的执
我正在尝试在 scala 中运行这样的 bash 命令: cat "example file.txt" | grep abc Scala 有一个特殊的流程管道语法,所以这是我的第一个方法: val f
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我需要一些帮助来理解并发编程的基础知识。事实上,我读得越多,就越感到困惑。因此,我理解进程是顺序执行的程序的一个实例,并且它可以由一个或多个线程组成。在单核CPU中,一次只能执行一个线程,而在多核CP
我的问题是在上一次集成测试后服务器进程没有关闭。 在integration.rs中,我有: lazy_static! { static ref SERVER: Arc> = {
我正在使用 Scala scala.sys.process图书馆。 我知道我可以用 ! 捕获退出代码和输出 !!但是如果我想同时捕获两者呢? 我看过这个答案 https://stackoverflow
我正在开发一个C++类(MyClass.cpp),将其编译为动态共享库(MyClass.so)。 同一台Linux计算机上运行的两个不同应用程序将使用此共享库。 它们是两个不同的应用程序。它不是多线程
我在我的 C 程序中使用 recvfrom() 从多个客户端接收 UDP 数据包,这些客户端可以使用自定义用户名登录。一旦他们登录,我希望他们的用户名与唯一的客户端进程配对,这样服务器就可以通过数据包
如何更改程序,以便函数 function_delayed_1 和 function_delayed_2 仅同时执行一次: int main(int argc, char *argv[]) {
考虑这两个程序: //in #define MAX 50 int main(int argc, char* argv[]) { int *count; int fd=shm
请告诉我如何一次打开三个终端,这样我的项目就可以轻松执行,而不必打开三个终端三次然后运行三个exe文件。请问我们如何通过脚本来做到这一点,即打开三个终端并执行三个 exe 文件。 最佳答案 在后台运行
我编写了一个监控服务来跟踪一组进程,并在服务行为异常、内存使用率高、超出 CPU 运行时间等时发出通知。 这在我的本地计算机上运行良好,但我需要它指向远程机器并获取这些机器上的进程信息。 我的方法,在
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 8年前关闭。 Improve this qu
我有一个允许用户上传文件的应用程序。上传完成后,必须在服务器上完成许多处理步骤(解压、存储、验证等...),因此稍后会在一切完成后通过电子邮件通知用户。 我见过很多示例,其中 System.Compo
这个问题对很多人来说可能听起来很愚蠢,但我想对这个话题有一个清晰的理解。例如:当我们在 linux(ubuntu, x86) 上构建一个 C 程序时,它会在成功编译和链接过程后生成 a.out。 a.
ps -eaf | grep java 命令在这里不是识别进程是否是 java 进程的解决方案,因为执行此命令后我的许多 java 进程未在输出中列出。 最佳答案 简答(希望有人写一个更全面的): 获
我有几个与内核态和用户态的 Windows 进程相关的问题。 如果我有一个 hello world 应用程序和一个暴露新系统调用 foo() 的 hello world 驱动程序,我很好奇在内核模式下
我找不到很多关于 Windows 中不受信任的完整性级别的信息,对此有一些疑问: 是否有不受信任的完整性级别进程可以创建命名对象的地方? (互斥锁、事件等) 不受信任的完整性级别进程是否应该能够打开一
我是一名优秀的程序员,十分优秀!