Bash, awk and file descriptors

Hi guys,
I'm thinking about this piece of command lines:
[user@host ~]$ echo "first word" > test
[user@host ~]$ cat test
first word
[user@host ~]$ { rm ./test && awk -F' ' '{ print $2,$1 }' > ./test; } < ./test
[user@host ~]$ cat test
word first
[user@host ~]$ { awk -F' ' '{ print $2,$1 }' > ./test; } < ./test
[user@host ~]$ cat test
[user@host ~]$ ls -l test
-rw-r--r-- 1 user user 0 17 apr 22.06 test
I wonder why:
{ rm ./test && awk -F' ' '{ print $2,$1 }' > ./test; } < ./test
works as I expect (the file "test" have words in reverse order), while instead:
{ awk -F' ' '{ print $2,$1 }' > ./test; } < ./test
give a file "test" empty, with size 0.
Why and how the leding "rm" make the difference?
Thanks

Mine wrote:
bohoomil wrote:OP
AHAHAH... you're very amusing.
Op has many meanings, what of the thoundas I find on internet is the meaning /dev/zero was thinking?
OP=OPerating system
OP=Original Poster
OP=Operator Access
(and so on...)
Less sarcasm (offsite) and more pragmatism, please.
http://netforbeginners.about.com/od/int … -Is-OP.htm
Happy ?

Similar Messages

  • Basic TCP and file descriptor question

    Hi all,
    I have DS 5.1 sp 1 and Solaris 8.
    how does the configurable "nsslapd-maxdescriptors" and "ulimit -n" works ?
    I assume that :
    1. nsslapd-maxdescriptors <= "ulimit -n".
    Current configuration is, nsslapd-maxdescriptors = 4096 and "ulimit -n" = 256. There are "currentconnections: 3212". These crazy connections is the "connection pool" from weblogic and others. I still able to browse using "ldapsearch" and similiar tool like "ldapbrowser".
    2. "tcp_conn_req_max_q" is not directly related to these parameters, since it describes concurrent connections. My "tcp_conn_req_max_q" = 128.
    "nsslapd-threadnumber", (=512 currently). How is this related to "nsslapd-maxdescriptors" and "tcp_conn_req_max_q" ? It this threadnumber per client connection perspective or Sun DS perspective.
    can some1 shed a light on this ? or give me some reference to read.
    thanks alot,

    got it !
    It's all in reference guide.... should have ransack SUN repository first....
    cheers

  • Bad File Descriptor in /dev/fd/3, and 94Gb of disk space missing

    I noticed a few days ago, possibly as the result of a recent kernel panic, that I have a large chunk of hard drive space missing. The Finder reports that I have approximately 89Gb of free space, but using "df" reports that there is approximately 178Gb free. Using "du" doesn't report any unexpected huge files, so I tried running GrandPerspective. In addition to the usual file usage and free space, this shows a single 94Gb block of "miscellaneous used space".
    I then booted into Single User mode to run fsck on the startup drive. This reported several errors, and took 3 passes to repair the directory structure, but didn't recover the missing space. I have subsequently run TechTool Pro and DiskWarrior on the startup drive (both of which found various minor errors), but the 94Gb still refuses to show itself.
    I then tried using "find" to look for single large files, using "sudo find / -size +94371840" (anything larger than 90Gb), and I get the following errors:
    find: /dev/fd/3: Bad file descriptor
    find: /dev/fd/4: Not a directory
    find: /dev/fd/5: Not a directory
    After searching Google, a "Bad file descriptor" error points to an inode issue, that fsck cannot fix, but I don't know enough (read: anything) about inodes to risk running the clri command to zero the problem inode.
    Short of blanking the startup disk and installing from scratch (not an attractive option), is there anything I can do to fix the broken inode and recover the missing space?
    Any help appreciated.

    Drawing Business wrote:
    I then tried using "find" to look for single large files, using "sudo find / -size +94371840" (anything larger than 90Gb), and I get the following errors:
    find: /dev/fd/3: Bad file descriptor
    find: /dev/fd/4: Not a directory
    find: /dev/fd/5: Not a directory
    This is not an error and always happens with find unless you exclude the /dev hierarchy from the search. (Interestingly this seems to have gone away with 10.5??)
    To locate your missing space, try WhatSize. Another alternative which I have not used personally is Disk Inventory X.
    As an additional point, with 10.4 it is actually better to use Disk Utility, since it does more than fsck: Resolve startup issues and perform disk maintenance with Disk Utility and fsck, quote:
    Note: If you're using Mac OS X 10.4 or later, you should use Disk Utility instead of fsck, whenever possible.

  • Synchronous and asynchronous IO on the same file descriptor

    Hi does any body knows about problems in combining synchronous and asynchronous IO on the same file descriptor? It appears that I may miss SIGIOs in that case.

    I don't think there is any real easy way to do an insert. If writing to a file, I would use a FileWriter, a FileOutputStream, or a RandomAccessFile class. I don't know of any class that will automatically do an insert so you might have to write your own method that does this. This is pretty easy: use RandomAccessFile might be the best for most situations for doing this (ie, if you are inserting many times at different locations in the file). Just copy all the bytes from the point in the file you want to insert to the end of the file, and then write your bit, and then rewrite all the read bytes to the file again. Depending on how many times you are writing to the file and how big the file is, you may want to keep the file in memory at all times because writing to disk is extremely slowwww. If the file is huge, and you are inserting seldomly, then do what I said above. If the point of insertion is closer to the start of the file, then it might be a good idea to do the above but instead of copying the bytes from the point of insertion to the end of the file, copy the bytes from the start of the file to the point of insertion.

  • [SOLVED] Maximun TCP open connections and Open File Descriptors

    Hi,
    back when I was using XP I needed to patch a system file to increase max amount of open TCP connections - you surely know about this. I'm wondering how do I do the same in GNU/Linux and if that's real important here.
    Also, by default open files are set to 1024, how do I change that? I'm running x86_64 + KDE SC 4.4.2
    Last edited by martin77 (2010-04-12 04:17:27)

    Thanks for replying.
    I mean "open file handler" or in a better GNU/Linux terminology "open file descriptors" aka the maximun number of files that can be accessed at a given time.
    For instance, VMWare will need you increase them to 4096 to work properly - and I presume something in the order of 5120 would be ok.
    Following The Arch Way, already found the solution and want to share it with all of you fellow n00bs:
    1. you need to open for edit /etc/security/limits.conf file with root privileges
    2. at the end of the file add:
    * soft nofile nn
    * hard nofile nn
    where nn is the number of open file descriptors you want. I set them to 8196 (probably too high) so for instance it should read:
    * soft nofile 8192
    * hard nofile 8192
    Read the embedded help for a better understanding of this crucial configuration file.
    As usual, thank you very much to this great community, devs and users, best!
    Last edited by martin77 (2010-04-12 04:18:47)

  • How do I find the number of file descriptors in use by the system?

    Hey folks,
    I am trying to figure out how many file descriptors my Leopard system has in use. On FreeBSD, this is exposed via sysctl at the OID kern.open_files (or something close to that, can't recall exactly what the name is). I do see that OS X has kern.maxfiles, which gives the maximum number of file descriptors the system can have open, but I don't see a sysctl that tells me how many of the 12288 descriptors the kernel thinks are in use.
    There's lsof, but is that really the only way? And, I'm not even sure if I can just equate the number of lines from lsof to the number of in use descriptors. I don't think it's that easy (perhaps it is and I'm just over complicating things).
    So, anyone know where this information is?
    Thanks for your time.

    glsmith wrote:
    There's lsof, but is that really the only way? And, I'm not even sure if I can just equate the number of lines from lsof to the number of in use descriptors.
    Can't think of anything other than lsof right now. However:
    Only root can list all open files, all other users see only their own
    There is significant duplication, and
    All types of file descriptor are listed, which you may not want, so you need to consider filtering.
    As an example, the following will count all regular files opened by all users:
    sudo lsof | awk '/REG/ { print $NF }' | sort -u | wc -lIf you run it without the sudo, you get just your own open files.

  • Set file descriptor limit for xinetd initiated process

    I am starting the amanda backup service on clients through xinetd and we
    are hitting the open file limit, ie file descriptor limit.
    I have set resource controls for the user and I can see from the shell that
    the file descriptor limit has increased, but I have not figured out how to get
    the resource control change to apply to the daemon started by xinetd.
    The default of 256 file channels persists for the daemon, I need to increase
    that number.
    I have tried a wrapper script, clearly doing it incorrectly for Solaris 10/SMF
    services. That route didn't work, or is not as straight forward as it used to be.
    There is a more direct way ?
    Thanks - Brian

    Hi Brian,
    It appears with 32 bits applications. You have to use the enabler of the extended FILE facility /usr/lib/extendedFILE.so.1
    % ulimit -n
    256
    % echo 'rlim_fd_max/D' | mdb -k | awk '{ print $2 }'
    65536
    % ulimit -n
    65536
    % export LD_PRELOAD_32=/usr/lib/extendedFILE.so.1
    % ./your_32_bits_applicationMarco

  • Stdio - file descriptor limits - Solaris 10

    Hi
    New to Solaris from HP-UX and we are porting an application.
    I run into a problem whereby we run out of file descriptors - application is TCP/IP and file I/O intensive. Sometimes it happens, sometimes not.
    It manifests itself as an error when calling setsockopt.
    Increasing the file limits in /etc/system has not relieved the problem.
    A google suggests there is a hard limit of 255 on file descriptors for 32-bit applications using stdio - does this still apply? Any workarounds?
    Specs:
    Solaris 10 01/06
    SunOS saturn 5.10 Generic_118822-25 sun4u sparc SUNW,Sun-Fire-v240
    Thanks in advance.

    What shell do you start the application from?
    If you use sh/bash/ksh; type "ulimit -a" too see what limits the process will inherit from the shell, if the value for 'open files' is very low you can increase it with:
    ulimit -n <new value>
    for example;
    ulimit -n 4096
    if you are using tcsh/csh type "limit -h" to view the limits.
    The values you set in /etc/system is the maximum allowed amount of file descriptors per process, it means that the process is allowed to increase its own limit of open files til that limit, but it doesn't mean that the process gets the highest limit automatically.
    See also:
    man ulimit
    man setrlimit
    7/M.

  • Passing bash arguments and variables

    Ok, so I've been staring at this script for the past hour with no avail. Basically, I want to pass arguments to a C program I wrote, both with arguments of the bash script and variables calculated in the bash script. For some reason it's not working, here's the script where it is right now:
    #!/bin/bash
    # Wrapper script for finding modes, outputting data as Frequency vs Amplitude
    NUMPOS=$(cat $1 | awk '/^position/' | wc -l)
    LINES=$[$(cat $1 | wc -l)-5-$NUMPOS]
    NUMFREQ=$[$LINES/$NUMPOS]
    mkdir split_$1
    cp -t split_$1 drumdata $1
    cd split_$1
    ./drumdata $1 $NUMPOS $NUMFREQ
    rm drumdata $1
    This gives me the error: ./getModes.sh: line 19: 26946 Segmentation fault      ./drumdata $1 $NUMPOS $NUMFREQ
    So I tried a different method with array variables
    #!/bin/bash
    # Wrapper script for finding modes, outputting data as Frequency vs Amplitude
    NUMPOS=$(cat $1 | awk '/^position/' | wc -l)
    LINES=$[$(cat $1 | wc -l)-5-$NUMPOS]
    NUMFREQ=$[$LINES/$NUMPOS]
    args[0]=$1
    args[1]=$NUMPOS
    args[2]=$NUMFREQ
    mkdir split_$1
    cp -t split_$1 drumdata $1
    cd split_$1
    ./drumdata ${args[@]}
    rm drumdata $1
    Which gives me the error: ./testModes: line 22:  7945 Segmentation fault      ./drumdata "${args[@]}"
    Which makes me want to cry because I wrote a script to test array variable behavior:
    File Edit Options Buffers Tools Sh-Script Help
    #!/bin/bash
    args[0]=test
    args[1]=getModes.sh
    args[2]=drumdata.c
    cp -t asdf ${args[@]}
    And it works . Why aren't my variables being evaluated?

    Moved to General Programming.

  • RFFOCA_T: DME with file descriptor in first line (RBC)

    Hi All,
    I've customized the automatic payment run for a company located at Canada - including generated DME- file by the report RFFOCA_T. The DME file looks good - but sadly the house bank (RBC, Royal Bank of Scotland) is expecting two things different:
    "Different formats now exist for the Royal Bank and CIBC from the default CPA-005 specification.
    u2022 Type 'A' and 'Cu2019 records have been modified to handle RBC and CIBC
    u2022 A parameter was added to job submission to request the bank type
    This process has been revised to include two headers as part of the tape_header code segment.
    u2022 The first header must be the first line in the file and appear in the following format: $$AAPDCPA1464[PROD]NL$$
    u2022 The second header (positions 36 to 1464) must be filled with blanks, not zeros"
    (taken from "SCT Banner, Finance, Release Guide - January 2005, Release 7.0")
    In our DME-file the second header (position 36 to 1464) is correct, but the first header is completely missing.
    RBC wrote me in an email:: "The first line of the file needs the file descriptor ($$AAPDCPA1464[PROD]NL$$). The date format and the client number is correct. When the $$ file descriptor has been added please upload the TEST file":
    I could not find any solution at SAP/ OSS - can anybody help, please?
    Thanks a lot!
    Sandra.

    Hi Revi,
    I'm not sure if I understand you in the right way.
    I do not have a problem only with the $$ at the beginning. The whole first expected line as the file descriptor is missing. As we saw in the report code, it's not considered. At least I hope, there is a simple solution - like an update or else - but maybe we need to enlarge the report itself by a programmer?
    Thanks,
    Sandra

  • Using bash as your file manager?

    Hello,
    My belief is that all file managers suck. There are no exceptions to this. So, for the past few months, I've been sourcing a file with a bunch of tricks I've invented / found through browsing the web to make using just bash as a file manager much more convenient.
    Here's what I currently use:
    # fm v1.9.1 by Kiah Morante
    # A very simple file manager.
    # Depends on pycp/pymv, http://github.com/yannicklm/pycp and feh
    # 'source' this file in a BASH shell
    showHidden=0 # Hidden files not shown
    showDetails=0 # ls is replaced with ls -lh if showDetails is 1
    shopt -s autocd # cd to a dir just by typing its name
    PROMPT_COMMAND='[[ ${__new_wd:=$PWD} != $PWD ]] && list; __new_wd=$PWD' # ls after cding
    # Shortcuts
    source ~/.config/fm/shortcuts # Call all custom shortcuts
    alias ..='cd ..'
    alias ...='cd ../..'
    alias ....='cd ../../..'
    alias h='cd ~'
    alias n='cd "$n"'
    # Keybindings
    bind '"\C-l":"list\C-m"'
    bind '"\C-h":"hide\C-m"'
    bind '"\C-o":"details\C-m"'
    bind '"\C-f":"makedir\C-m"'
    bind '"\C-n":"n\C-m"'
    bind '"\C-y":"cpwd\C-m"'
    bind '"\C-p":"cd "$OLDPWD"\C-m"' # Hint: You could also type '~-'
    # FM prompt appearance
    if [[ $(whoami) == 'root' ]]; then
    # So that the user knows if they have root privileges:
    PS1="\[\e[0;32\]mf\[m\e[m\] \[\e[0;31m\]root\[\e[m\] \[\e[0;34m\]\w \[\e[m\]\[\e[0;31m\]> \[\e[m\]"
    else
    PS1="\[\e[0;32\]mf\[m\e[m\] \[\e[0;34m\]\w \[\e[m\]\[\e[0;31m\]> \[\e[m\]"
    fi
    # Functions
    # Usage
    fmhelp () {
    echo "hide - toggle hidden (hidden by default)
    ls - lists contents of dir(s) passed in args.
    lsd - list directories
    cd - changed to directory \$1
    cp \$@ \$2 - copies file from \$1 to \$2
    mv \$@ \$2 - moves file from \$1 to \$2
    rm \$@ - deletes \$@
    sc \$1 \$2 - make a shortcut called \$1 pointing to \$2. If no \$2 is passed, it is evaluated as \$PWD
    cpwd - copy current working directory
    .., ..., .... - cd .. etc.
    o \$1 - opens \$1 with xdg-open
    hm - how many files are in the current directory
    details - show file details (ls -lh)
    fmhelp - this help menu
    n - Intelligent guess of the next dir you wish to cd to. Last $1 in open, list, or makedir; last argument in copy or move; pwd before a cd
    ~- - BASH shortcut for \$OLDPWD
    img - feh frontend with the following usage:
    img -t \$2 - views the dirs/images specified in \$2..\$n as clickable thumbnails
    img -s \$2 \$3 - views the images specified in \$3..\$n as a slideshow with a slide change speed of \$2 seconds
    img \$@ - views the dirs/images specified
    Shortkeys:
    Ctrl-f - mkdir
    Ctrl-h - hide
    Ctrl-l - ls
    Ctrl-n - cd \$n
    Ctrl-o - details
    Ctrl-p - cd \$OLDPWD
    Ctrl-y - cpwd
    Ctrl-u - clear line # urxvt default"
    # Toggle display hidden files
    # If $showHidden is 1, hidden files are shown
    hide () {
    showHidden=$(( 1 - $showHidden ))
    list
    # Toggle display file details
    # If $showDetails is 1, file details are shown
    details () {
    showDetails=$(( 1 - $showDetails ))
    list
    # ls
    listToggle () {
    if [[ $showHidden == 1 && $showDetails == 1 ]]; then
    ls -C --color -A -lh "$dir"
    elif [[ $showHidden == 1 && $showDetails == 0 ]]; then
    ls -C --color -A "$dir"
    elif [[ $showHidden == 0 && $showDetails == 1 ]]; then
    ls -C --color -lh "$dir"
    else
    ls -C --color "$dir"
    fi
    list () {
    clear # Unclutter the screen
    # List pwd if no $1
    if [[ $@ == "" ]]; then
    set '.'
    fi
    # List multiple folders:
    for dir in "$@"
    do
    listToggle
    done
    n="$1" # See 'n' in fmhelp
    # use feh to view thumbnails/images/slideshow
    img () {
    case "$1" in
    -t) nohup feh --thumbnails "${@:2}" --thumb-height 120 --thumb-width 120 -S filename -d --cache-thumbnails -B black > /dev/null 2>&1 & ;;
    -s) nohup feh "${@:3}" -S filename -d -B black --slideshow-delay "$2" > /dev/null 2>&1 & ;;
    *) nohup feh "$@" -S filename -d -B black > /dev/null 2>&1 & ;;
    esac
    list
    # cp
    copy () {
    if [[ $showHidden == 1 ]]; then
    pycp --interactive --all "$@"
    else
    pycp --interactive "$@"
    fi
    list
    n="${@:(-1)}" # n is the last argument (where stuff is moved to)
    # mv
    move () {
    if [[ $showHidden == 1 ]]; then
    pymv --interactive --all "$@"
    else
    pymv --interactive "$@"
    fi
    list
    n="${@:(-1)}"
    makedir () {
    if [[ $1 == "" ]]; then
    read -e n
    set "$n"
    fi
    if mkdir -- "$1"; then
    list # Update pwd to show new dir(s) that have been made.
    n="$1"
    fi
    # rm
    remove () {
    rm -rfI "$@"
    list
    # open files
    o () {
    # To use xdg-open
    #nohup xdg-open "$1" > /dev/null 2>&1 &
    if [ -f "$1" ] ; then
    case "$1" in
    *.tar.bz2) tar xjf "$1" ;;
    *.tar.gz) tar xzf "$1" ;;
    *.bz2) bunzip2 "$1" ;;
    *.rar) rar x "$1" ;;
    *.gz) gunzip "$1" ;;
    *.tar) tar xf "$1" ;;
    *.tbz2) tar xjf "$1" ;;
    *.tgz) tar xzf "$1" ;;
    *.zip) unzip "$1" ;;
    *.Z) uncompress "$1" ;;
    *.7z) 7z x "$1" ;;
    *.pdf) nohup zathura "$1" > /dev/null 2>&1 & ;;
    *.html) nohup luakit "$1" > /dev/null 2>&1 & ;;
    *.blend) nohup blender "$1" > /dev/null 2>&1 & ;;
    *.avi) nohup mplayer "$1" ;;
    *.wmv) nohup mplayer "$1" ;;
    *.rmvb) nohup mplayer "$1" ;;
    *.mp3) nohup urxvtc -si -sw -sh 30 -e mplayer "$1" > /dev/null 2>&1 & ;;
    *.flv) nohup mplayer "$1" ;;
    *.mp4) nohup mplayer "$1" ;;
    *.ogg) nohup urxvt -si -sw -sh 30 -e mplayer "$1" > /dev/null 2>&1 & ;;
    *.wav) nohup audacity "$1" > /dev/null 2>&1 & ;;
    *.jpg) img "$1" ;;
    *.jpeg) img "$1" ;;
    *.JPG) img "$1" ;;
    *.png) img "$1" ;;
    *.gif) nohup gpicview "$1" > /dev/null 2>&1 & ;;
    *) nohup urxvt -si -sw -sh 30 -e vim "$1" > /dev/null 2>&1 & ;;
    esac
    else
    echo "'$1' is not a valid file"
    fi
    n="$1"
    # Add shortcuts
    makeShortcut () {
    if [[ $2 == "" ]]; then
    set $1 .
    fi
    echo ""$1"=\""$2"\"
    alias "$1"='cd \""$2"\"'
    " >> ~/.config/fm/shortcuts
    source ~/.config/fm/shortcuts
    # Copy pwd to clipboard
    cpwd () {
    echo \"$(pwd)\" | xclip
    # List directories
    lsd () {
    ls -F "$@" | grep \/$
    # Command aliases
    alias mv="move"
    alias sc="makeShortcut"
    alias cp="copy"
    alias ls="list"
    alias rm="remove"
    alias mkdir="makedir"
    alias hm="ls -l . | egrep -c '^-'"
    list # ls when fm starts
    Could all of you fellow file manager-haters post your little tricks, whether just a few lines added to ~/.bashrc or fully fledged files that you source like mine?
    Last edited by greenmanwitch (2011-02-07 19:58:40)

    3]) wrote: once you have video files cluttered all throughout your hard drive and folders all over, thats where the 'bash' filemanager system lacks its use in terms of effectiveness.
    Actually, I found this to be one of the best advantages of using bash is that it forces a user to think about file organization and making useful naming schemes for files.
    For example, instead of having 1000+ media files in one directory I subcategorize theme by genre or whatever, and then probably subcategorize them again.
    Then I usually rename the files to something meaningful, like if I have 50 pictures of my kids birthday, just do a for each loop on the directory and rename all the files donovan_birthdayX.jpg where X is an integer incrementation.
    essentially. just don't "have files cluttered all throughout you hard drive and folders all over". and your life will be much happier regardless of how you manage your files.

  • "IOException: Bad file descriptor" thrown during readline()

    I'm working on a system to send data to bluetooth devices. Currently I have a dummy program that "finds" bluetooth devices by listening for input on System.in, and when one is found, the system sends some data to the device over bluetooth. Here is the code for listening for input on System.in
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    boolean streamOpen = true;
    while(streamOpen) {
         String next = "";
         System.out.println("waiting for Input: ");
         try {
              next = br.readLine();
               // other code here
          } catch (IOException ioe) {
               ioe.printStackTrace();
    } // end of whileThis is running in it's own thread, constantly listening for input from System.in. There is also another thread that handles pushing the data to the bluetooth device. It works the first time it reads input, then the other thread starts running also, printing output to System.out. When the data has successfully been pushed to the device, the system waits for me to enter more information. As soon as I type something and press return, i get an endless (probably infinte if I don't kill the process) list of IOExceptions:Bad file descriptor exceptions that are thrown from the readline() method.
    Here is what is being printed:
    Waiting for Input: // <-- This is the thread listening for input on System.in
    system started with 1 Bluetooth Chip // From here down is the thread that pushing data to the BT device
    next device used 0
    default device 0000000000
    start SDP for 0000AA112233
    *** obex_push: 00:00:AA:11:22:33@9, path/to/file.txt, file.txt
    I'm not even sure which line it's trying to read when the exception gets thrown, whether it's the first line after "Waiting for Input: " or it's the line where I actually type something and hit return.
    Any ideas why this might be happening? Could it have something to do with reading from System.in from a thread that is not the main thread?
    Also, this is using java 1.6

    Actually, restarting the stream doesn't work either..... here's a sample program that I wrote.
    public class ExitListener extends Thread {
         private BufferedReader br;
         private boolean threadRunning;
         public ExitListener(UbiBoardINRIA ubiBoard) {
              super("Exit Listener");
              threadRunning = true;
              InputStreamReader isr = new InputStreamReader(System.in);
              br = new BufferedReader(isr);
         public void run() {
              while (threadRunning) {
                   try {
                        String read = br.readLine();
                        if (read.equalsIgnoreCase("Exit")) {
                             threadRunning = false;
                   } catch (IOException ioe) {
                        System.out.println("Can you repeat that?");
                        try {
                             br.close();
                             br = new BufferedReader(new InputStreamReader(System.in));
                        } catch (IOException ioe2) {
                             ioe2.printStackTrace();
                             System.out.println("Killing this thread");
                             threadRunning = false;
                   } // end of catch
         } // end of run
    }output:
    I'm sorry, can you repeat that command? - Stream closed
    Closed Stream
    Ready?: false
    I'm sorry, can you repeat that command? - Stream closed
    Closed Stream
    Ready?: false
    I'm sorry, can you repeat that command? - Stream closed
    Closed Stream
    Ready?: false
    I'm sorry, can you repeat that command? - Stream closed
    Closed Stream
    Ready?: false
    I know that this is probably not enough code to really see the problem, but my main question is what could be going on somewhere else in the code that could cause this BufferedReader to not be able to re-open

  • Suddenly can't create dirs or files!? "Bad file descriptor"

    Tearing my hair out...
    Suddenly, neither root nor users can create files or directories in directories under /home. Attempting to do so gets: Error -51 in Finder, "Bad file descriptor" from command line, and "Invalid file handle" via SMB.
    However, files and dirs can be: read, edited, moved, and occasionally copied. Rebooting made no difference.
    Anyone have a clue on where to start on this?
    Mac OS X 10.3.9. Dual G4 XServe with 2 x 7 x 250 G XRAID.
    Many   Mac OS X (10.3.9)   Many
    Many   Mac OS X (10.3.9)   Many

    Indeed. This whole episode has exposed rather woeful lack of robustness on the part of the X Server and XRAID... various things failing and server hanging completely as a result of a few bad files on disk.. with lack of useful feedback as to what was happening.
    Best I can tell, we had reached the stage where the next available disk location for directory or file was bad... blocking any further additions.
    I've embarked on the process of copying everything off, remove crash-provoking files, replace one bad drive (hot swap didn't work), erase all, perform surface conditioning (bad-block finding) procedure, and maybe later this century will be copying all files back.
    Looks to me like the bad block finding procedure is finding a few bad blocks on the supposedly good drives... presumably will isolate those, but maybe we need to get more new drives.
    Many   Mac OS X (10.3.9)   Many

  • Problem with file descriptors not released by JMF

    Hi,
    I have a problem with file descriptors not released by JMF. My application opens a video file, creates a DataSource and a DataProcessor and the video frames generated are transmitted using the RTP protocol. Once video transmission ends up, if we stop and close the DataProcessor associated to the DataSource, the file descriptor identifying the video file is not released (checkable through /proc/pid/fd). If we repeat this processing once and again, the process reaches the maximum number of file descriptors allowed by the operating system.
    The same problem has been reproduced with JMF-2.1.1e-Linux in several environments:
    - Red Hat 7.3, Fedora Core 4
    - jdk1.5.0_04, j2re1.4.2, j2sdk1.4.2, Blackdown Java
    This is part of the source code:
    // video.avi with tracks audio(PCMU) and video(H263)
    String url="video.avi";
    if ((ml = new MediaLocator(url)) == null) {
    Logger.log(ambito,refTrazas+"Cannot build media locator from: " + url);
    try {
    // Create a DataSource given the media locator.
    Logger.log(ambito,refTrazas+"Creating JMF data source");
    try
    ds = Manager.createDataSource(ml);
    catch (Exception e) {
    Logger.log(ambito,refTrazas+"Cannot create DataSource from: " + ml);
    return 1;
    p = Manager.createProcessor(ds);
    } catch (Exception e) {
    Logger.log(ambito,refTrazas+"Failed to create a processor from the given url: " + e);
    return 1;
    } // end try-catch
    p.addControllerListener(this);
    Logger.log(ambito,refTrazas+"Configure Processor.");
    // Put the Processor into configured state.
    p.configure();
    if (!waitForState(p.Configured))
    Logger.log(ambito,refTrazas+"Failed to configure the processor.");
    p.close();
    p=null;
    return 1;
    Logger.log(ambito,refTrazas+"Configured Processor OK.");
    // So I can use it as a player.
    p.setContentDescriptor(new FileTypeDescriptor(FileTypeDescriptor.RAW_RTP));
    // videoTrack: track control for the video track
    DrawFrame draw= new DrawFrame(this);
    // Instantiate and set the frame access codec to the data flow path.
    try {
    Codec codec[] = {
    draw,
    new com.sun.media.codec.video.colorspace.JavaRGBToYUV(),
    new com.ibm.media.codec.video.h263.NativeEncoder()};
    videoTrack.setCodecChain(codec);
    } catch (UnsupportedPlugInException e) {
    Logger.log(ambito,refTrazas+"The processor does not support effects.");
    } // end try-catch CodecChain creation
    p.realize();
    if (!waitForState(p.Realized))
    Logger.log(ambito,refTrazas+"Failed to realize the processor.");
    return 1;
    Logger.log(ambito,refTrazas+"realized processor OK.");
    /* After realize processor: THESE LINES OF SOURCE CODE DOES NOT RELEASE ITS FILE DESCRIPTOR !!!!!
    p.stop();
    p.deallocate();
    p.close();
    return 0;
    // It continues up to the end of the transmission, properly drawing each video frame and transmit them
    Logger.log(ambito,refTrazas+" Create Transmit.");
    try {
    int result = createTransmitter();
    } catch (Exception e) {
    Logger.log(ambito,refTrazas+"Error Create Transmitter.");
    return 1;
    } // end try-catch transmitter
    Logger.log(ambito,refTrazas+"Start Procesor.");
    // Start the processor.
    p.start();
    return 0;
    } // end of main code
    * stop when event "EndOfMediaEvent"
    public int stop () {
    try {   
    /* THIS PIECE OF CODE AND VARIATIONS HAVE BEEN TESTED
    AND THE FILE DESCRIPTOR IS NEVER RELEASED */
    p.stop();
    p.deallocate();
    p.close();
    p= null;
    for (int i = 0; i < rtpMgrs.length; i++)
    if (rtpMgrs==null) continue;
    Logger.log(ambito, refTrazas + "removeTargets;");
    rtpMgrs[i].removeTargets( "Session ended.");
    rtpMgrs[i].dispose();
    rtpMgrs[i]=null;
    } catch (Exception e) {
    Logger.log(ambito,refTrazas+"Error Stoping:"+e);
    return 1;
    return 0;
    } // end of stop()
    * Controller Listener.
    public void controllerUpdate(ControllerEvent evt) {
    Logger.log(ambito,refTrazas+"\nControllerEvent."+evt.toString());
    if (evt instanceof ConfigureCompleteEvent ||
    evt instanceof RealizeCompleteEvent ||
    evt instanceof PrefetchCompleteEvent) {
    synchronized (waitSync) {
    stateTransitionOK = true;
    waitSync.notifyAll();
    } else if (evt instanceof ResourceUnavailableEvent) {
    synchronized (waitSync) {
    stateTransitionOK = false;
    waitSync.notifyAll();
    } else if (evt instanceof EndOfMediaEvent) {
    Logger.log(ambito,refTrazas+"\nEvento EndOfMediaEvent.");
    this.stop();
    else if (evt instanceof ControllerClosedEvent)
    Logger.log(ambito,refTrazas+"\nEvent ControllerClosedEvent");
    close = true;
    waitSync.notifyAll();
    else if (evt instanceof StopByRequestEvent)
    Logger.log(ambito,refTrazas+"\nEvent StopByRequestEvent");
    stop =true;
    waitSync.notifyAll();
    Many thanks.

    Its a bug on H263, if you test it without h263 track or with other video codec, the release will be ok.
    You can try to use a not-Sun h263 codec like the one from fobs or jffmpeg projects.

  • Where is the file descriptor leak in this code?

    The following "appendStringToFile" method is used to append a String to a file. My java app calls this method a few times per minute, and then crashes after running for about 12 hours. The exception is "Too many open files". The code that calls it does so from a synchronized block, so concurrency is not the problem, and it would seem that only one file descriptor should be used at a time.
    Can anyone find the problem?
         public static void createParentDirectoryIfNeeded(String path) {
              String dirPath = path.substring(0, path.lastIndexOf('/'));
              File dir = new File(dirPath);
              if (!dir.exists()) {
                   dir.mkdirs();
         public static void appendStringToFile(String s, String path) {
              FileWriter fileWriter = null;
              try {
                   createParentDirectoryIfNeeded(path);
                   // create file if it doesn't already exist
                   File file = new File(path);
                   file.createNewFile();
                   if (s != null) {
                        fileWriter = new FileWriter(file, true);
                        fileWriter.write(s.toCharArray());
              } catch (IOException ioe) {
                   ErrorHandler.handleError(ioe, LOG);
              } finally {
                   if (fileWriter != null) {
                        try {
                             fileWriter.close();
                        } catch (IOException ioe) {
                             ErrorHandler.handleError(ioe, LOG);
         }Edited by: mikewertheim on Sep 26, 2008 11:54 AM
    Edited by: mikewertheim on Sep 26, 2008 11:54 AM
    Edited by: mikewertheim on Sep 26, 2008 11:55 AM
    Edited by: mikewertheim on Sep 26, 2008 11:56 AM

    I don't know what is causing your problem but I can suggest several improvements.
    1) Given a file 'f' then one can create the parent directory using   f.getParentFile().mkdirs();so that one does not need to useString dirPath = path.substring(0, path.lastIndexOf('/'));          2) There is not need to test if the directory exists before creating it. If it exists then   f.getParentFile().mkdirs(); will just do nothing.
    3) There is no need to usefile.createNewFile();because if the file does not exist then fileWriter = new FileWriter(file, true);will create it.

Maybe you are looking for