top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

What's wrong in this code?? This code is about Limiting the maximum size of file the process can create

+4 votes
196 views

Code to write data in to file example.txt.. which is working fine

// basic file operations
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}

File : filelimit.c below which takes input as the compiled binary of above code
In this code i am setting the file size limit of process to 10 bytes and the above code is trying to write more than 10 bytes. According to below code if a process is trying to write more than 10 bytes to file it should send the signal SIGXFSZ (see more about this here)

It is not sending here... Why??

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

void handler(int sig)
{
    if(sig == SIGXFSZ)
        printf("Inside handler with correct signal\n");
    else
        printf("Inside handler with wrong signal\n");
}


int main(int argc, char const *argv[])
{
    pid_t pid;
    pid = fork();

    if(pid == 0)
    {
        signal(SIGXFSZ, handler);

        char command[256];
        scanf("%s", command);

        int ret;

        struct rlimit rl;
        rl.rlim_cur = 10;
        rl.rlim_max = 10;
        setrlimit (RLIMIT_FSIZE, &rl);

        char *envp[] = { NULL };
        char *argv[] = { command , NULL };
        ret = execve(command, argv, envp);

        while(1);
    }
    else
    {
        printf("%d - %d\n", getpid(), pid);
        signal(SIGXFSZ, handler);
        while(1);
    }
    return 0;
}
posted Mar 8, 2014 by Raghu

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button
I tried to test the code and saw that the file example.txt is created with 10 bytes only i.e "Writing th" which is expected, can u please explain what u are expecting.
Now I got ur question, so I tried the following program
I tried with the following program -

    int main()
    {
            char command[]="./b"; // b is my binary name from previous file
            struct rlimit rl;
    
            signal(SIGXFSZ, handler);
            rl.rlim_cur = 10;
            rl.rlim_max = 10;
            setrlimit (RLIMIT_FSIZE, &rl);
    
            char *envp[] = { NULL };
            char *argv[] = { command , NULL };
    
            execve(command, argv, envp);
            while(1);
    
            return 0;
    }

Output

    File size limit exceeded

So now the conclusion is it is something to do with the fork and signal handling, which is in WIP, but nice question...
@Salil Yes, i'm asking for catching the file limit exceeded signal
@Raghu that's what I tried without forking and it worked.
But i want with fork. Please let me know if you find the answer with fork
Not sure if serves your purpose, changed the behaviour of child and parent process and in parent process it worked I mean signal raised and cought properly.

Done few experiment and tried to send the signal explicitly to child process which was working properly

So the summary is it is still not solved and my take is setrlimit is working properly but does not raise a signal if it is called from child process this could be because of some (additional) setting required or a bug :(

Similar Questions
+4 votes

Lets assume I have a system with RAM of 1GB. and virtual memory is 500MB. That brings to 1.5GB i.e. 1500 MBytes.

I have read somewhere that when I process is created stack of 8MB is associated to that process. So, assuming that any of the process is not allocating any dynamic memory or anything, then does it mean that, Maximum number of process that i can create is 1500/8 and i.e. 187 Process.

Please clarify my understanding,

+2 votes

I want a script which -
1. Monitor is list of process in Linux.
2. When it is any process is down it can log the error and restart the process.
3. List of the processes can be supplied as the command line parameters.

+1 vote

As far as i know when a process is created, 8MB of stack is allocated to it.
Is it right?

If not then is there any fix size or not?

If yes then please answer and Help To solve this question also.

+3 votes

Please understand the scenario :

I have an embedded system, on which i do telnet and then i starts i run an application using ./binary_name &.
Now if i close the terminal, and if i do telnet from new terminal then i can see above process is still running.

Now, To check this, i have written a sample program,

#include<stdio.h>
main()
{
    while(1);
}

I just compiled it and i ran it on my linux PC using ./a.out &.
Then i just closed the terminal and from new terminal i checked using ps -elf command this process was killed.

My question is why different behavior for both process. i started both the process in background. Then why?

Any suggestion?

Thanks in advance

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...