"STATUS: Too many open files in system" 打开并锁定
"STATUS: Too many open files in system" with open and lock
在linux和C语言中,我有两个函数可以通过5个进程(T1,T2,...,T5)锁定16个文件(MA1,MA2,...,MA16)。使用 acquire(...) 进程 - 我锁定文件(如果尚未被其他进程锁定),将 0 写入 1,然后解锁文件。使用 release(...) 进程锁定文件,将 1 写入 0,然后解锁文件。当我 运行 使用 fork() 时,我从打开 "STATUS: Too many open files in system"
得到以下错误。在 linux 中,最大值为 1024,但我的 5 个进程
还远未达到该限制
alessandro@LinuxAle:~$ ulimit -n
1024
密码是:
子 exe
#include <string.h>
#include "accessory.h"
#include "logfilemanager.h"
#include "lockmanager.h"
#include <sys/types.h>
#include <signal.h>
#define TRAIN_INITIALS "T"
#define SIZE 256
void next(int step, int *route, int size);
int main(int argc , char *argv[])
{
char *name[2];
int *route;
int fdlog;
char * logprint;
memset(name, '[=12=]', sizeof(name));
strcpy(name, TRAIN_INITIALS);
strcat(name, argv[1]);
route = get_route(name, "", SIZE);
print_route(name, route);
fdlog = create_logfile(name, SIZE);
for(int i = 1; i < (route[0]+1); i++)
{
logprint = update_logfileETC1(fdlog, i, route);
printf("Train %s: %s", name, logprint);
fflush(stdout);
next(i, route, SIZE);
}
close(fdlog);
return 0;
}
void next(int step, int *route, int size) {
char * next_track_name;
char * actual_track_name;
char * next_file_path_name;
char * actual_file_path_name;
int status;
int file_exist;
/* acquire */
next_track_name = get_name(route[step+1]);
next_file_path_name = get_file_name(next_track_name, "", size);
file_exist = access(next_file_path_name, F_OK); // check if file exist
if(file_exist == 0)
{
status = acquire(next_file_path_name);
while(status == -1)
{
status = acquire(next_file_path_name);
}
}
sleep(3);
/* release */
actual_track_name = get_name(route[step]);
actual_file_path_name = get_file_name(actual_track_name, "", size);
file_exist = access(actual_file_path_name, F_OK); // check if file exist
if(file_exist == 0)
{
status = release(actual_file_path_name);
while(status == -1)
{
status = release(actual_file_path_name);
}
}
}
锁定文件的获取和释放
include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <libgen.h>
int acquire(char *fname)
{
char c;
size_t nbytes;
int status;
int fd;
int close_return;
struct flock lock; //Create an variable of type struct flock to define the properties of locking
nbytes = sizeof(c);
fd = open(fname, O_RDWR, (mode_t)777);
if (fd == -1)
{
//fd = open(fname, O_RDWR);
printf("acquire file: %s\n", fname);
fflush(stdout);
perror("STATUS");
exit(1);
}
memset(&lock, 0, sizeof(lock));
lock.l_type = F_WRLCK;
lock.l_pid = getpid();
read(fd, &c, nbytes);
if((c - '0') == 1){
return -1;
}
/* lock the file */
status = fcntl(fd, F_SETLK, &lock);
if(status == -1)
{
return -1;
}
lseek(fd, 0L, SEEK_SET);
read(fd, &c, nbytes);
if((c - '0') == 0)
{
lseek(fd, 0L, SEEK_SET);
write(fd, "1", nbytes);
}
else if ((c - '0') == 1)
{
printf("Resource %s is already acquired\n", fname);
lock.l_type = F_ULOCK;
if (fcntl(fd, F_SETLKW, &lock) == -1 ) {
perror("fcntl caused some error: ");
exit(1);
}
}
/* Release the lock */
lock.l_type = F_ULOCK;
if (fcntl(fd, F_SETLKW, &lock) == -1 ) {
perror("fcntl caused some error: ");
exit(1);
}
if((close_return = close(fd)) < 0)
{
perror("close");
exit(1);
}
return 0;
}
int release(char *fname)
{
char c;
size_t nbytes;
int status;
int fd;
int close_return;
struct flock lock; //Create an variable of type struct flock to define the properties of locking
nbytes = sizeof(c);
fd = open(fname, O_RDWR, (mode_t)777);
if (fd == -1)
{
//fd = open(fname, O_RDWR);
printf("release file: %s\n", fname);
fflush(stdout);
perror("STATUS");
exit(1);
}
memset(&lock, 0, sizeof(lock));
lock.l_type = F_WRLCK;
lock.l_pid = getpid();
/* lock the file */
status = fcntl(fd, F_SETLK, &lock);
if(status == -1)
{
return -1;
}
lseek(fd, 0L, SEEK_SET);
write(fd, "0", nbytes);
/* Release the lock */
lock.l_type = F_ULOCK;
if (fcntl(fd, F_SETLKW, &lock) == -1 ) {
perror("fcntl caused some error: ");
exit(EXIT_FAILURE);
}
if((close_return = close(fd)) < 0)
{
perror("close");
exit(1);
}
return 0;
}
带分叉的主要是:
#include <stdio.h>
#include <signal.h>
#include <sys/wait.h>
#include "accessory.h"
#include <sys/types.h>
#include <signal.h>
#define NUMBER_TRACKS 16
#define NUMBER_STATIONS 8
#define NUMBER_TRAINS 5
#define TRACKS_INITIALS "MA"
#define STATION_INITIALS "S"
#define SIZE 256
#define CHILDETCONE "childETCone"
int main(int argc , char *argv[])
{
pid_t pid;
char track_name[2];
char track_number[2];
int execl_return;
char index[2];
char * execl_path_name;
/* create the MAx file initialized to zero */
for(int i = 1; i < (NUMBER_TRACKS+1); i++)
{
memset(track_name, '[=14=]', sizeof(track_name));
memset(track_number, '[=14=]', sizeof(track_number));
strcpy(track_name, TRACKS_INITIALS);
sprintf(track_number, "%d", i);
strcat(track_name, track_number);
create_track_file(track_name, "", SIZE);
}
execl_path_name = get_file_name(CHILDETCONE, "", SIZE);
printf("path %p\n", execl_path_name);
for(int i = 0; i < NUMBER_TRAINS; i++)
{
pid = fork();
if (pid < 0)
{
perror("fork");
exit(1);
}
if (pid == 0) //child
{
sprintf(index, "%d", i+1);
execl_return = execl(execl_path_name, CHILDETCONE, index, NULL);
if(execl_return == -1)
{
perror("execl");
exit(1);
}
exit(0);
}
}
for (int i = 0; i < NUMBER_TRAINS; i++)
{
wait(NULL);
}
return 0;
结果是:
Route of train T5: [S5, MA4, MA3, MA2, MA1, S1]
Train T5: [Attuale: S5], [Next: MA4], 7 Agosto 2018 0:35:31
Route of train T4: [S6, MA8, MA3, MA2, MA1, S1]
Train T4: [Attuale: S6], [Next: MA8], 7 Agosto 2018 0:35:31
Route of train T3: [S4, MA14, MA15, MA16, MA12, S8]
Train T3: [Attuale: S4], [Next: MA14], 7 Agosto 2018 0:35:31
Route of train T2: [S3, MA9, MA10, MA11, MA12, S8]
Train T2: [Attuale: S3], [Next: MA9], 7 Agosto 2018 0:35:31
Route of train T1: [S2, MA5, MA6, MA7, MA3, MA8, S6]
Train T1: [Attuale: S2], [Next: MA5], 7 Agosto 2018 0:35:31
Train T4: [Attuale: MA8], [Next: MA3], 7 Agosto 2018 0:35:34
Train T5: [Attuale: MA4], [Next: MA3], 7 Agosto 2018 0:35:34
Train T3: [Attuale: MA14], [Next: MA15], 7 Agosto 2018 0:35:34
Train T2: [Attuale: MA9], [Next: MA10], 7 Agosto 2018 0:35:34
Train T1: [Attuale: MA5], [Next: MA6], 7 Agosto 2018 0:35:34
acquire file: /home/alessandro/CLionProjects/ETC1/cmake-build-debug/MA3
STATUS: Too many open files in system
Train T2: [Attuale: MA10], [Next: MA11], 7 Agosto 2018 0:35:37
Train T1: [Attuale: MA6], [Next: MA7], 7 Agosto 2018 0:35:37
Train T4: [Attuale: MA3], [Next: MA2], 7 Agosto 2018 0:35:37
Train T3: [Attuale: MA15], [Next: MA16], 7 Agosto 2018 0:35:37
Train T3: [Attuale: MA16], [Next: MA12], 7 Agosto 2018 0:35:40
Train T4: [Attuale: MA2], [Next: MA1], 7 Agosto 2018 0:35:40
Train T2: [Attuale: MA11], [Next: MA12], 7 Agosto 2018 0:35:40
Train T1: [Attuale: MA7], [Next: MA3], 7 Agosto 2018 0:35:40
acquire file: /home/alessandro/CLionProjects/ETC1/cmake-build-debug/MA12
STATUS: Too many open files in system
Train T3: [Attuale: MA12], [Next: S8], 7 Agosto 2018 0:35:43
Train T4: [Attuale: MA1], [Next: S1], 7 Agosto 2018 0:35:43
Train T1: [Attuale: MA3], [Next: MA8], 7 Agosto 2018 0:35:43
Train T3: [Attuale: S8], [Next: --], 7 Agosto 2018 0:35:46
Train T4: [Attuale: S1], [Next: --], 7 Agosto 2018 0:35:46
Train T1: [Attuale: MA8], [Next: S6], 7 Agosto 2018 0:35:46
Train T1: [Attuale: S6], [Next: --], 7 Agosto 2018 0:35:49
Process finished with exit code 0
在acquire
中,你做:
if((c - '0') == 1){
return -1;
}
/* lock the file */
status = fcntl(fd, F_SETLK, &lock);
if(status == -1)
{
return -1;
}
如果这些 if
语句中的任何一个 return,您将从函数 中 return 而不会 关闭打开的文件描述符。
注意:因为你没有post你的整个子程序代码(即execle
的目标),所以很难说是什么这肯定会产生影响,但非常值得怀疑。
更新:
I diposted the child code
好的,这清楚地证实了错误。如果 either 一个 returns -1:
- 这意味着您将在每次后续调用中开始用完文件描述符。
- 你可能会得到一个 [接近] 无限循环,因为
next
如果收到 -1 return 会再次 [又] 调用它们
只需添加:
close(fd);
在你上面的return -1
语句之前。
请注意 release
也有类似的 if (status == -1) return -1
问题。
在linux和C语言中,我有两个函数可以通过5个进程(T1,T2,...,T5)锁定16个文件(MA1,MA2,...,MA16)。使用 acquire(...) 进程 - 我锁定文件(如果尚未被其他进程锁定),将 0 写入 1,然后解锁文件。使用 release(...) 进程锁定文件,将 1 写入 0,然后解锁文件。当我 运行 使用 fork() 时,我从打开 "STATUS: Too many open files in system"
得到以下错误。在 linux 中,最大值为 1024,但我的 5 个进程
alessandro@LinuxAle:~$ ulimit -n
1024
密码是:
子 exe
#include <string.h>
#include "accessory.h"
#include "logfilemanager.h"
#include "lockmanager.h"
#include <sys/types.h>
#include <signal.h>
#define TRAIN_INITIALS "T"
#define SIZE 256
void next(int step, int *route, int size);
int main(int argc , char *argv[])
{
char *name[2];
int *route;
int fdlog;
char * logprint;
memset(name, '[=12=]', sizeof(name));
strcpy(name, TRAIN_INITIALS);
strcat(name, argv[1]);
route = get_route(name, "", SIZE);
print_route(name, route);
fdlog = create_logfile(name, SIZE);
for(int i = 1; i < (route[0]+1); i++)
{
logprint = update_logfileETC1(fdlog, i, route);
printf("Train %s: %s", name, logprint);
fflush(stdout);
next(i, route, SIZE);
}
close(fdlog);
return 0;
}
void next(int step, int *route, int size) {
char * next_track_name;
char * actual_track_name;
char * next_file_path_name;
char * actual_file_path_name;
int status;
int file_exist;
/* acquire */
next_track_name = get_name(route[step+1]);
next_file_path_name = get_file_name(next_track_name, "", size);
file_exist = access(next_file_path_name, F_OK); // check if file exist
if(file_exist == 0)
{
status = acquire(next_file_path_name);
while(status == -1)
{
status = acquire(next_file_path_name);
}
}
sleep(3);
/* release */
actual_track_name = get_name(route[step]);
actual_file_path_name = get_file_name(actual_track_name, "", size);
file_exist = access(actual_file_path_name, F_OK); // check if file exist
if(file_exist == 0)
{
status = release(actual_file_path_name);
while(status == -1)
{
status = release(actual_file_path_name);
}
}
}
锁定文件的获取和释放
include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <libgen.h>
int acquire(char *fname)
{
char c;
size_t nbytes;
int status;
int fd;
int close_return;
struct flock lock; //Create an variable of type struct flock to define the properties of locking
nbytes = sizeof(c);
fd = open(fname, O_RDWR, (mode_t)777);
if (fd == -1)
{
//fd = open(fname, O_RDWR);
printf("acquire file: %s\n", fname);
fflush(stdout);
perror("STATUS");
exit(1);
}
memset(&lock, 0, sizeof(lock));
lock.l_type = F_WRLCK;
lock.l_pid = getpid();
read(fd, &c, nbytes);
if((c - '0') == 1){
return -1;
}
/* lock the file */
status = fcntl(fd, F_SETLK, &lock);
if(status == -1)
{
return -1;
}
lseek(fd, 0L, SEEK_SET);
read(fd, &c, nbytes);
if((c - '0') == 0)
{
lseek(fd, 0L, SEEK_SET);
write(fd, "1", nbytes);
}
else if ((c - '0') == 1)
{
printf("Resource %s is already acquired\n", fname);
lock.l_type = F_ULOCK;
if (fcntl(fd, F_SETLKW, &lock) == -1 ) {
perror("fcntl caused some error: ");
exit(1);
}
}
/* Release the lock */
lock.l_type = F_ULOCK;
if (fcntl(fd, F_SETLKW, &lock) == -1 ) {
perror("fcntl caused some error: ");
exit(1);
}
if((close_return = close(fd)) < 0)
{
perror("close");
exit(1);
}
return 0;
}
int release(char *fname)
{
char c;
size_t nbytes;
int status;
int fd;
int close_return;
struct flock lock; //Create an variable of type struct flock to define the properties of locking
nbytes = sizeof(c);
fd = open(fname, O_RDWR, (mode_t)777);
if (fd == -1)
{
//fd = open(fname, O_RDWR);
printf("release file: %s\n", fname);
fflush(stdout);
perror("STATUS");
exit(1);
}
memset(&lock, 0, sizeof(lock));
lock.l_type = F_WRLCK;
lock.l_pid = getpid();
/* lock the file */
status = fcntl(fd, F_SETLK, &lock);
if(status == -1)
{
return -1;
}
lseek(fd, 0L, SEEK_SET);
write(fd, "0", nbytes);
/* Release the lock */
lock.l_type = F_ULOCK;
if (fcntl(fd, F_SETLKW, &lock) == -1 ) {
perror("fcntl caused some error: ");
exit(EXIT_FAILURE);
}
if((close_return = close(fd)) < 0)
{
perror("close");
exit(1);
}
return 0;
}
带分叉的主要是:
#include <stdio.h>
#include <signal.h>
#include <sys/wait.h>
#include "accessory.h"
#include <sys/types.h>
#include <signal.h>
#define NUMBER_TRACKS 16
#define NUMBER_STATIONS 8
#define NUMBER_TRAINS 5
#define TRACKS_INITIALS "MA"
#define STATION_INITIALS "S"
#define SIZE 256
#define CHILDETCONE "childETCone"
int main(int argc , char *argv[])
{
pid_t pid;
char track_name[2];
char track_number[2];
int execl_return;
char index[2];
char * execl_path_name;
/* create the MAx file initialized to zero */
for(int i = 1; i < (NUMBER_TRACKS+1); i++)
{
memset(track_name, '[=14=]', sizeof(track_name));
memset(track_number, '[=14=]', sizeof(track_number));
strcpy(track_name, TRACKS_INITIALS);
sprintf(track_number, "%d", i);
strcat(track_name, track_number);
create_track_file(track_name, "", SIZE);
}
execl_path_name = get_file_name(CHILDETCONE, "", SIZE);
printf("path %p\n", execl_path_name);
for(int i = 0; i < NUMBER_TRAINS; i++)
{
pid = fork();
if (pid < 0)
{
perror("fork");
exit(1);
}
if (pid == 0) //child
{
sprintf(index, "%d", i+1);
execl_return = execl(execl_path_name, CHILDETCONE, index, NULL);
if(execl_return == -1)
{
perror("execl");
exit(1);
}
exit(0);
}
}
for (int i = 0; i < NUMBER_TRAINS; i++)
{
wait(NULL);
}
return 0;
结果是:
Route of train T5: [S5, MA4, MA3, MA2, MA1, S1]
Train T5: [Attuale: S5], [Next: MA4], 7 Agosto 2018 0:35:31
Route of train T4: [S6, MA8, MA3, MA2, MA1, S1]
Train T4: [Attuale: S6], [Next: MA8], 7 Agosto 2018 0:35:31
Route of train T3: [S4, MA14, MA15, MA16, MA12, S8]
Train T3: [Attuale: S4], [Next: MA14], 7 Agosto 2018 0:35:31
Route of train T2: [S3, MA9, MA10, MA11, MA12, S8]
Train T2: [Attuale: S3], [Next: MA9], 7 Agosto 2018 0:35:31
Route of train T1: [S2, MA5, MA6, MA7, MA3, MA8, S6]
Train T1: [Attuale: S2], [Next: MA5], 7 Agosto 2018 0:35:31
Train T4: [Attuale: MA8], [Next: MA3], 7 Agosto 2018 0:35:34
Train T5: [Attuale: MA4], [Next: MA3], 7 Agosto 2018 0:35:34
Train T3: [Attuale: MA14], [Next: MA15], 7 Agosto 2018 0:35:34
Train T2: [Attuale: MA9], [Next: MA10], 7 Agosto 2018 0:35:34
Train T1: [Attuale: MA5], [Next: MA6], 7 Agosto 2018 0:35:34
acquire file: /home/alessandro/CLionProjects/ETC1/cmake-build-debug/MA3
STATUS: Too many open files in system
Train T2: [Attuale: MA10], [Next: MA11], 7 Agosto 2018 0:35:37
Train T1: [Attuale: MA6], [Next: MA7], 7 Agosto 2018 0:35:37
Train T4: [Attuale: MA3], [Next: MA2], 7 Agosto 2018 0:35:37
Train T3: [Attuale: MA15], [Next: MA16], 7 Agosto 2018 0:35:37
Train T3: [Attuale: MA16], [Next: MA12], 7 Agosto 2018 0:35:40
Train T4: [Attuale: MA2], [Next: MA1], 7 Agosto 2018 0:35:40
Train T2: [Attuale: MA11], [Next: MA12], 7 Agosto 2018 0:35:40
Train T1: [Attuale: MA7], [Next: MA3], 7 Agosto 2018 0:35:40
acquire file: /home/alessandro/CLionProjects/ETC1/cmake-build-debug/MA12
STATUS: Too many open files in system
Train T3: [Attuale: MA12], [Next: S8], 7 Agosto 2018 0:35:43
Train T4: [Attuale: MA1], [Next: S1], 7 Agosto 2018 0:35:43
Train T1: [Attuale: MA3], [Next: MA8], 7 Agosto 2018 0:35:43
Train T3: [Attuale: S8], [Next: --], 7 Agosto 2018 0:35:46
Train T4: [Attuale: S1], [Next: --], 7 Agosto 2018 0:35:46
Train T1: [Attuale: MA8], [Next: S6], 7 Agosto 2018 0:35:46
Train T1: [Attuale: S6], [Next: --], 7 Agosto 2018 0:35:49
Process finished with exit code 0
在acquire
中,你做:
if((c - '0') == 1){
return -1;
}
/* lock the file */
status = fcntl(fd, F_SETLK, &lock);
if(status == -1)
{
return -1;
}
如果这些 if
语句中的任何一个 return,您将从函数 中 return 而不会 关闭打开的文件描述符。
注意:因为你没有post你的整个子程序代码(即execle
的目标),所以很难说是什么这肯定会产生影响,但非常值得怀疑。
更新:
I diposted the child code
好的,这清楚地证实了错误。如果 either 一个 returns -1:
- 这意味着您将在每次后续调用中开始用完文件描述符。
- 你可能会得到一个 [接近] 无限循环,因为
next
如果收到 -1 return 会再次 [又] 调用它们
只需添加:
close(fd);
在你上面的return -1
语句之前。
请注意 release
也有类似的 if (status == -1) return -1
问题。