提问人:Kenny Ynnek 提问时间:4/5/2023 更新时间:8/24/2023 访问量:185
Visual Studio Code 上的 .nfsXXX 文件
.nfsXXX files on visual studio code
问:
服务器:
我目前在远程 linux 服务器中工作。
问题:
每次我运行代码时,都会有一些 .nfsXXX 文件生成到我的工作目录中。
我的 Makefile:
CC=gcc
CFLAGS=-g -lm
problem2 : problem2.o helpers.o
$(CC) -o problem2 helpers.o problem2.o -pthread -I/path/to/header/files
.PHONY : clean
clean :
rm *.o $(objects) problem2
代码运行良好,没有任何错误。 如何摆脱它们?为什么会这样?
我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <semaphore.h>
#include <pthread.h>
#include <sys/shm.h>
#include <errno.h>
#include <assert.h>
#include <dirent.h>
#include "helpers.h"
#define BUFFER_SIZE 10
/**
* @brief This function recursively traverse the source directory.
*
* @param dir_name : The source directory name.
*/
char **file_paths;
int file_count = 0;
int word_count = 0;
void traverseDir(char *dir_name);
int main(int argc, char **argv)
{
int process_id;
file_paths = malloc(100 * sizeof(char *));
char *dir_name = argv[1];
if (argc < 2){
printf("Main process: Please enter a source directory name.\nUsage: ./main <dir_name>\n");
exit(-1);
}
traverseDir(dir_name);
if (file_count == 0)
{
printf("Main process: No txt files found in the source directory.\n");
exit(-1);
}
if (file_count > 100)
{
printf("Main process: The number of txt files is greater than 100.\n");
exit(-1);
}
printf("Found %d files in the source directory.\n", file_count);
for (int i = 0; i < file_count; i++)
{
printf("%s\n", file_paths[i]);
}
int shmid;
int f,e,m;
char *shared_buffer;
shmid = shmget(IPC_PRIVATE, BUFFER_SIZE, 0666 | IPC_CREAT);
shared_buffer = (char *)shmat(shmid, 0, 0);
sem_t *_full_, *_empty_, *_mutex_;
f = shmget(IPC_PRIVATE, sizeof(sem_t), 0666 | IPC_CREAT);
e = shmget(IPC_PRIVATE, sizeof(sem_t), 0666 | IPC_CREAT);
m = shmget(IPC_PRIVATE, sizeof(sem_t), 0666 | IPC_CREAT);
_full_ = (sem_t *)shmat(f, 0, 0);
_empty_ = (sem_t *)shmat(e, 0, 0);
sem_init(_full_, 1, 0);
sem_init(_empty_, 1, 0);
int *read_file_count;
int rKey = shmget(IPC_PRIVATE, sizeof(int), 0666 | IPC_CREAT);
read_file_count = (int *)shmat(rKey, 0, 0);
*read_file_count = 0;
size_t* num_read;
int nKey = shmget(IPC_PRIVATE, sizeof(size_t), 0666 | IPC_CREAT);
num_read = (size_t *)shmat(nKey, 0, 0);
*num_read = 0;
switch (process_id = fork())
{
default:
while(*read_file_count < file_count){
FILE *fp = fopen(file_paths[*read_file_count],"r");
if( fp == NULL ){
printf("Fail to open file!\n");
exit(0);
} else {
}
size_t total_read = 0;
printf("Parent:\n");
printf("++++++++++Reading FILE %d++++++++++\n", *read_file_count);
while((*num_read = fread(shared_buffer, sizeof(char), BUFFER_SIZE, fp)) > 0) {
printf("[Parent] Buffer of size %d contains: %.*s\n",
(int)(*num_read), (int)(*num_read), shared_buffer);
printf("------------------------------\n");
sem_post(_full_); //printf("Parent process released full.\n");
sem_wait(_empty_); //printf("Parent process acquired empty.\n");
}
fclose(fp);
(*read_file_count)++;
}
printf("Parent process: Finished.\n");
printf("Parent process: The total number of words is %d.\n", word_count);
break;
case 0:
sem_wait(_full_); printf("Child process got full+1. ");
while(*read_file_count < file_count){
printf("Child:\n");
printf("[Child] Buffer of size %d contains: %.*s\n",
(int)(*num_read), (int)(*num_read), shared_buffer);
printf("previous word count: %d\n", word_count);
char* my_string = (char*)malloc((*num_read + 1) * sizeof(char));
memcpy(my_string, shared_buffer, *num_read);
my_string[*num_read] = '\0';
word_count += wordCount(my_string);
if(wordCount(my_string) != 1 && my_string[(*num_read)-1] != ' '){
word_count--;
}
free(my_string);
printf("child counted words : %d\n", word_count);
printf("------------------------------\n");
sem_post(_empty_); //printf("Child process released empty.\n");
sem_wait(_full_); //printf("Child process got full. ");
}
printf("Child process: Finished.\n");
exit(0);
case -1:
printf("Fork failed!\n");
exit(-1);
}
exit(0);
}
/**
* @brief This function recursively traverse the source directory.
*
* @param dir_name : The source directory name.
*/
void traverseDir(char *dir_name)
{
DIR *dir;
struct dirent *ent;
if ((dir = opendir(dir_name)) != NULL)
{
while ((ent = readdir(dir)) != NULL)
{
if (ent->d_type == DT_DIR)
{
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
{
continue;
}
char *new_dir_name = malloc(strlen(dir_name) + strlen(ent->d_name) + 2);
strcpy(new_dir_name, dir_name);
strcat(new_dir_name, "/");
strcat(new_dir_name, ent->d_name);
traverseDir(new_dir_name);
}
else
{
if (strstr(ent->d_name, ".txt") != NULL)
{
char *new_file_path = malloc(strlen(dir_name) + strlen(ent->d_name) + 2);
strcpy(new_file_path, dir_name);
strcat(new_file_path, "/");
strcat(new_file_path, ent->d_name);
file_paths[file_count] = new_file_path;
file_count++;
}
}
}
closedir(dir);
}
else
{
perror("");
return;
}
}
我实际上用谷歌搜索了一下,但我找不到将它们从 Visual Studio 代码中删除的方法,而且大多数关于 nfs 的描述都有点雄心勃勃,我不知道我的代码的哪一部分发生了这种情况,因为我应该在打开后正确关闭所有文件。
答:
-1赞
CoderAtWork
8/24/2023
#1
NFS 是一个共享文件的系统。正常工作时,.nfs 文件在使用后会删除。有时,当文件崩溃时,.nfs 文件会保留下来。
在此处了解更多信息:https://www.datto.com/blog/what-is-nfs-file-share
答要删除文件,请关闭所有打开的文件,然后在终端中键入以下内容:
pkill -9 -u (用户名)
这将杀死终端,因此请确保事先保存所需的所有内容。这也将影响您的终端历史记录。
评论
.nfs file