LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-20-2004, 08:23 AM   #1
FunkMeisterMaz
LQ Newbie
 
Registered: Mar 2004
Distribution: Mandrake 10
Posts: 2

Rep: Reputation: 0
Unhappy getrlimit is setting soft and hard limits to -1, why?


Hello everyone, this is my first post to LinuxQuestions. I am learning to program in Linux, using C, from the book Beginning Linux Programming by Matthew and Stones. One of the examples in the book deals with setting the soft and hard limits for the resources. For some reason when I run the program, typed exactly as it is in the book, I am getting the value -1 for both the rlim_cur and rlim_max data members. I have checked that the getrlimit function did not fail, as it did not return -1.
Below is my code, can anybody explain to me what is happening? Many thanks.
Maz

#include <math.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>


void work()
{
FILE *f;
int i;
double x = 4.5;

f = tmpfile();
for(i=0; i<10000; i++) {
fprintf(f, "Do some output\n");
if(ferror(f)) {
fprintf(stderr, "Error writing to temporary file\n");
exit(1);
}
}

for(i=0; i<1000000; i++)
x = log(x*x + 3.21);
}

int main()
{
struct rusage r_usage;
struct rlimit r_limit;
int priority;

work();
getrusage(RUSAGE_SELF, &r_usage);

printf("CPU usage: Use = %ld.%06ld, System = %ld.%06ld\n",
r_usage.ru_utime.tv_sec, r_usage.ru_utime.tv_usec,
r_usage.ru_stime.tv_sec, r_usage.ru_stime.tv_usec);

priority = getpriority(PRIO_PROCESS, getpid());
printf("Current priority = %d\n", priority);

if((getrlimit(RLIMIT_FSIZE, &r_limit))<0)
printf("Error here!");

printf("Current FSIZE limit: soft = %ld, hard = %ld\n",
r_limit.rlim_cur, r_limit.rlim_max);

r_limit.rlim_cur = 2048;
r_limit.rlim_max = 4096;
printf("Setting a 2K file size limit\n");
setrlimit(RLIMIT_FSIZE, &r_limit);

work();
exit(0);
}
 
Old 03-22-2004, 02:31 AM   #2
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 418

Rep: Reputation: 31
Welcome to LQ FunkMeisterMaz,

RLIM_INFINITY, which is equal to -1, is the value of resources that have no limit. So, if RLIMIT_FSIZE's soft and hard limit = -1, that means there is no limit on file sizes.

understand?
 
Old 03-22-2004, 07:34 AM   #3
FunkMeisterMaz
LQ Newbie
 
Registered: Mar 2004
Distribution: Mandrake 10
Posts: 2

Original Poster
Rep: Reputation: 0
I see, thank you very much! Now it makes sense :-)

maz
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting limits nelamvr6 Linux - Security 9 10-24-2005 07:38 PM
Soft vs. Hard Links jrdioko Linux - Newbie 2 06-13-2004 03:41 PM
Hard/Soft Links nodger Linux - General 1 05-15-2004 11:36 AM
Which do you prefer. Hard / soft copy text? unholy General 7 04-14-2004 06:37 PM
Purpose of setting a soft limit ? vmniza Linux - General 1 03-11-2004 08:37 AM


All times are GMT -5. The time now is 09:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration